I am trying to save an string array using NSUserDefaults with swift. I have searched around and believe that i have to use [NSString] not [String] however the app still crashes with error
fatal error: unexpectedly found nil while unwrapping an Optional value
I can not see what i am doing wrong as it is working perfectly fine saving ints and strings. Here is my code for my data singleton and NSUserDefaults.
struct DefaultsKeys
{
static let myString = "myString"
static let myInt = "myInt"
static let myArray = "myArray"
}
class DataContainerSingleton
{
static let sharedDataContainer = DataContainerSingleton()
var myString: String?
var myInt: Int?
var myArray:[NSString]?
/* I have also tried:
var myArray:[NSString] = [NSString]() */
var goToBackgroundObserver: AnyObject?
init()
{
let defaults = NSUserDefaults.standardUserDefaults()
myString = defaults.objectForKey(DefaultsKeys.myString) as! String?
myInt = defaults.objectForKey(DefaultsKeys.myInt) as! Int?
myArray = defaults.objectForKey(DefaultsKeys.myArray) as! [NSString]?
goToBackgroundObserver = NSNotificationCenter.defaultCenter().addObserverForName(
UIApplicationDidEnterBackgroundNotification,
object: nil,
queue: nil)
{
(note: NSNotification!) -> Void in
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject( self.myString, forKey: DefaultsKeys.my)
defaults.setObject( self.myInt, forKey: DefaultsKeys.myInt)
defaults.setObject( self.myArray, forKey: DefaultsKeys.myArray)
defaults.synchronize()
}
}
}
<!-- language: swift -->
//declare and initiate the array
var sample_array : NSMutableArray! = []
//use mutable array to add data
//variable to store value in array
var say: String! = "Hello"
//add values in array
sample_array.addObject("hello")
//or
//sample_array.addObject(say)
/*
//you can also save values in array like this
var sample_array: Array<String> = []
var say: String! = "Hello"
sample_array = ["Qwerty"]
sample_array.insert(say, atIndex: 1)
println(sample_array)
*/
//archive and saving data
let data = NSKeyedArchiver.archivedDataWithRootObject(sample_array)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "array_key")
NSUserDefaults.standardUserDefaults().synchronize()
//unarchive and getting data
if let data = NSUserDefaults.standardUserDefaults().objectForKey("array_key") as? NSData {
stopover_array = NSKeyedUnarchiver.unarchiveObjectWithData(data) as NSMutableArray
}
Problem is the code:
myString = defaults.objectForKey(DefaultsKeys.myInt) as! String?
myInt = defaults.objectForKey(DefaultsKeys.myInt) as! Int?
myArray = defaults.objectForKey(DefaultsKeys.myArray) as! [NSString]?
as!
means unwraping an optional value, that's why your app crashed with error
fatal error: unexpectedly found nil while unwrapping an Optional value
if defaults.objectForKey(DefaultsKeys.myInt)
is nil, then unwrapping nil is not allowed. You should use
myString = defaults.objectForKey(DefaultsKeys.myInt) as? String
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.