User defaults gives nil value in Swift 3.0 - ios

I am new to swift and trying to store NSMutableArray in Userdefaults. Here is what I am doinig :
//to save aaray in user defaults
var set:NSMutableArray = NSMutableArray(objects: self.listId)
self.saveListIdArray(set)
func saveListIdArray(_ params: NSMutableArray = []) {
let defaults = UserDefaults.standard
defaults.set(params, forKey: "ArrayKey")
defaults.synchronize()
}
//to get array from user default
func getUserDefaultKeyForAllObject(key userDefaultsKey: String) -> NSMutableArray {
let array = UserDefaults.standard.object(forKey: NSUserDefaultsKey.LIST_ID_ARRAY) as! NSMutableArray
print(array)
return array
}
App crashes with "fatal error: unexpectedly found nil while unwrapping an Optional value" exception.
Ignore the way I ask the question, help me out here.
Thank you.

Try converting to NSData then storing to nsuserdefaults like below
func saveListIdArray(_ params: NSMutableArray = []) {
let data = NSKeyedArchiver.archivedData(withRootObject: params)
UserDefaults.standard.set(data, forKey: "test")
UserDefaults.standard.synchronize()
}
For retrieving the data use
if let data = UserDefaults.standard.object(forKey: "test") as? Data {
if let storedData = NSKeyedUnarchiver.unarchiveObject(with: data) as? NSMutableArray
{
// In here you can access your array
}
}

You are force unwrapping the NSMutableArray for a key. Don't force unwrap when you try to get the value from a dictionary or UserDefault for a key because there may be a chance that the value does not exist for that key and force unwrapping will crash your app.
Do this as:
//to get array from user default
if let array = UserDefaults.standard.object(forKey:"ArrayKey") as? NSMutableArray
print(array)
}

I have 2 possible reasons for this:
You need to be 100% sure that you are retrieving array with the same key as you save it with. In your code you are saving the array with "ArrayKey" but retrieving it with NSUserDefaultsKey.LIST_ID_ARRAY, are you sure this is the same string?
What datatype is self.listId? If it's a custom class then you need to make that class conform to the nscoding protocol, then encode it to Data and save that to the userDefaults (Save custom objects into NSUserDefaults)
A 3rd reason is that you are trying to get an object from the defaults without ever writing anything to it. Try changing
let array = UserDefaults.standard.object(forKey: NSUserDefaultsKey.LIST_ID_ARRAY) as! NSMutableArray
print(array)
return array
to
if let array = UserDefaults.standard.object(forKey: "ArrayKey") as? NSMutableArray {
print(array)
return array
}
else {
return NSMutableArray()
}

Related

How to parse Object in preference with swift? [duplicate]

I am trying to store an array to NSUserDefaults and retrieve the array when needed to populate a UITableView.
Currently I am using:
//store data
NSUserDefaults.standardUserDefaults().setObject(myArray, forKey: "\(identity.text!)listA")
NSUserDefaults.standardUserDefaults().synchronize()
//retrieve data
let tabledata = NSUserDefaults.standardUserDefaults().stringForKey("\(identity.text!)listA")
myArray = [tabledata!]
tableView.reloadData()
But I get
fatal error: unexpectedly found nil while unwrapping an Optional
value
when trying to load the data. I am not sure if the issue is in the storage or the retrieval. Has anyone been through this before?
From your code I see you are storing some array
// Your code
NSUserDefaults.standardUserDefaults().setObject(myArray, forKey: "\(identity.text!)listA")
and retrieving a string
//Your code
let tabledata = NSUserDefaults.standardUserDefaults().stringForKey("\(identity.text!)listA")
There is probably a type mismatch, You store one type and retrieve another type.
While retrieving either use arrayForKey() or objectForKey() see the code below.
let tabledata = NSUserDefaults.standardUserDefaults().arrayForKey("\(identity.text!)listA")
or
let tabledata = NSUserDefaults.standardUserDefaults().objectForKey("\(identity.text!)listA")
If it is an array I would go with First one.
Here are the way that you can store and retrieved array of object with help of NSKeyArchive.
To Store array to NSUserDefault.
let placesData = NSKeyedArchiver.archivedData(withRootObject: placesArray)
UserDefaults.standard.set(placesData, forKey: "places")
To Retrieved array from NSUserDefault.
let placesData = UserDefaults.standard.object(forKey: "places") as? NSData
if let placesData = placesData {
let placesArray = NSKeyedUnarchiver.unarchiveObject(with: placesData as Data) as? [Place]
print(placesArray)
}
Hope this help you.
Saving and Retrieving a String array to UserDefaults
Save array
let array = ["horse", "cow", "camel", "sheep", "goat"]
let defaults = UserDefaults.standard
defaults.set(array, forKey: "SavedStringArray")
Retrieve array
let defaults = UserDefaults.standard
let array = defaults.object(forKey: "SavedStringArray") as? [String] ?? [String]()
See more at How to save user settings using NSUserDefaults
What are you storing in the array?
If it's objects it won't work, if It's just strings or numbers then it should be fine.
Next steps you take should be to turn on exception breakpoints and maybe println() everything you can so you can pinpoint the exact whereabouts of the problem.
As a shot in the dark I would suggest maybe (identity.text!) is coming out as nil
Also you should probably change it to this
if let tabledata: NSArray = NSUserDefaults.standardUserDefaults().stringForKey("\(identity.text!)listA"){
myArray = [tabledata!]
tableView.reloadData()
}
This will make the code not even try to run unless there is a value it can use
Latest swift3 version to store array of data into userdefaults and get that array of data using keys
let nameArray = ["Ramu","JAGAN","Steve","Swift"]
let namesArrayData = NSKeyedArchiver.archivedData(withRootObject: nameArray)
UserDefaults.standard.set(namesArrayData, forKey: "arrayData")
RetriveData
let retriveArrayData= UserDefaults.standard.object(forKey: "arrayData") as? NSData
if let retriveArrayData= namesArrayData{
let retriveArray = NSKeyedUnarchiver.unarchiveObject(with: namesArraydata as Data) as? [nameArray]
print(retriveArray )
}
This has been changed in swift 3.
Note that this works with device but not playground or simulator
//store the array
let array = ["horse", "cow", "camel", "sheep", "goat"]
let defaults = UserDefaults.standard
defaults.setValue(array, forKey: "history")
defaults.synchronize()
//Retrieve the array
if((UserDefaults.standard.array(forKey: "history")) != nil){
let historyWords = (UserDefaults.standard.array(forKey: "history") as? [String])!
print(historyWords)
}
Store into User Default:
let arrayFruit = ["Apple","Banana","Orange","Grapes","Watermelon"]
//store in user default
UserDefaults.standard.set(arrayFruit, forKey: "arrayFruit")
Retrieving array from User Defaults:
if let arr = UserDefaults.standard.array(forKey: "arrayFruit") as? [String]{
print(arr)
}

How to store Array in NSUserDefault in Swift?

So I've been trying for a few hours for a way to store an array to NSUserDefault and print them to the cells but the data is not saving as an array, it basically only saves one value at a time.
var emailData = [String]()
var passwordData = [String]()
#IBAction func addPressed(sender: AnyObject) {
let defaults = NSUserDefaults.standardUserDefaults()
emailData.append(addEmail.text!)
passwordData.append(addPassword.text!)
var storedEmail = defaults.objectForKey("emailData") as? [String] ?? [String]()
var storedPasswords = defaults.objectForKey("passwordData") as? [String] ?? [String]()
// then update whats in the `NSUserDefault`
defaults.setObject(emailData, forKey: "emailData")
defaults.setObject(passwordData, forKey: "passwordData")
// call this after you update
defaults.synchronize() /*
NSUserDefaults.standardUserDefaults().setValue(addEmail.text, forKey: "email")
NSUserDefaults.standardUserDefaults().synchronize()
*/
}
So basically the data is not saving.
You can store Array too, by this way
let userDefault = NSUserDefaults.standardUserDefaults()
let arr = ["abc","xyz","pqr"]
userDefault.setObject(arr, forKey: "arr")
userDefault.synchronize()
let data = userDefault.objectForKey("arr") as! [String]
Save the Swift Array in Swift.
let kUserDefault = NSUserDefaults.standardUserDefaults()
kUserDefault.setObject(["KIRIT" , "MODI" , "FIRST" , "LAST"], forKey: "nameArray")
kUserDefault.synchronize()
Get Array
1. arrayForKey : You getting Swift Array
kUserDefault.arrayForKey("nameArray")!
2. objectForKey : You getting NSArray
kUserDefault.objectForKey("nameArray")!
3. valueForKey : You getting NSArray
kUserDefault.valueForKey("nameArray")
It is possible to store arrays in NSUserDefaults. But separating email and password in separate array is not suitable. You should store it as one object since they are related.
In your case an array of Dictionary objects is better, which is possible to store in NSUserDefaults as well, which you can retrieve as an array of Dictionary.
Here is the sample:
let userDefault = NSUserDefaults.standardUserDefaults()
var storedCredentials = userDefault.objectForKey("credentials") as? [[String:AnyObject]] ?? [[String:AnyObject]]()
let email =
[
"email" : addEmail.text!,
"password": addPassword.text!
]
storedCredentials.append(email)
userDefault.setObject(storedCredentials, forKey: "credentials")
userDefault.synchronize()
You can't save an array of [String] in NSUserDefault, if you look at the documentation :
For NSArray and NSDictionary objects, their contents must be property
list objects.
So what you can do is convert your [String] into NSData then save it into NSUserDefault.
// Store it as NSData
var emailData = [String]()
emailData.append("email1")
emailData.append("email2")
let emailIntoNSData = NSKeyedArchiver.archivedDataWithRootObject(emailData)
NSUserDefaults.standardUserDefaults().setObject(emailIntoNSData, forKey: "emailData")
// Retrieve it :
let emailFromNSData = NSUserDefaults.standardUserDefaults().objectForKey("emailData") as? NSData
if let emailFromNSData = emailFromNSData {
let emailArray = NSKeyedUnarchiver.unarchiveObjectWithData(emailFromNSData) as? [String]
if let emailArray = emailArray {
NSLog("Email Data : \(emailArray)") // ["email1","email2"]
// do something…
}
}

(Swift) Storing and retrieving Array to NSUserDefaults

I am trying to store an array to NSUserDefaults and retrieve the array when needed to populate a UITableView.
Currently I am using:
//store data
NSUserDefaults.standardUserDefaults().setObject(myArray, forKey: "\(identity.text!)listA")
NSUserDefaults.standardUserDefaults().synchronize()
//retrieve data
let tabledata = NSUserDefaults.standardUserDefaults().stringForKey("\(identity.text!)listA")
myArray = [tabledata!]
tableView.reloadData()
But I get
fatal error: unexpectedly found nil while unwrapping an Optional
value
when trying to load the data. I am not sure if the issue is in the storage or the retrieval. Has anyone been through this before?
From your code I see you are storing some array
// Your code
NSUserDefaults.standardUserDefaults().setObject(myArray, forKey: "\(identity.text!)listA")
and retrieving a string
//Your code
let tabledata = NSUserDefaults.standardUserDefaults().stringForKey("\(identity.text!)listA")
There is probably a type mismatch, You store one type and retrieve another type.
While retrieving either use arrayForKey() or objectForKey() see the code below.
let tabledata = NSUserDefaults.standardUserDefaults().arrayForKey("\(identity.text!)listA")
or
let tabledata = NSUserDefaults.standardUserDefaults().objectForKey("\(identity.text!)listA")
If it is an array I would go with First one.
Here are the way that you can store and retrieved array of object with help of NSKeyArchive.
To Store array to NSUserDefault.
let placesData = NSKeyedArchiver.archivedData(withRootObject: placesArray)
UserDefaults.standard.set(placesData, forKey: "places")
To Retrieved array from NSUserDefault.
let placesData = UserDefaults.standard.object(forKey: "places") as? NSData
if let placesData = placesData {
let placesArray = NSKeyedUnarchiver.unarchiveObject(with: placesData as Data) as? [Place]
print(placesArray)
}
Hope this help you.
Saving and Retrieving a String array to UserDefaults
Save array
let array = ["horse", "cow", "camel", "sheep", "goat"]
let defaults = UserDefaults.standard
defaults.set(array, forKey: "SavedStringArray")
Retrieve array
let defaults = UserDefaults.standard
let array = defaults.object(forKey: "SavedStringArray") as? [String] ?? [String]()
See more at How to save user settings using NSUserDefaults
What are you storing in the array?
If it's objects it won't work, if It's just strings or numbers then it should be fine.
Next steps you take should be to turn on exception breakpoints and maybe println() everything you can so you can pinpoint the exact whereabouts of the problem.
As a shot in the dark I would suggest maybe (identity.text!) is coming out as nil
Also you should probably change it to this
if let tabledata: NSArray = NSUserDefaults.standardUserDefaults().stringForKey("\(identity.text!)listA"){
myArray = [tabledata!]
tableView.reloadData()
}
This will make the code not even try to run unless there is a value it can use
Latest swift3 version to store array of data into userdefaults and get that array of data using keys
let nameArray = ["Ramu","JAGAN","Steve","Swift"]
let namesArrayData = NSKeyedArchiver.archivedData(withRootObject: nameArray)
UserDefaults.standard.set(namesArrayData, forKey: "arrayData")
RetriveData
let retriveArrayData= UserDefaults.standard.object(forKey: "arrayData") as? NSData
if let retriveArrayData= namesArrayData{
let retriveArray = NSKeyedUnarchiver.unarchiveObject(with: namesArraydata as Data) as? [nameArray]
print(retriveArray )
}
This has been changed in swift 3.
Note that this works with device but not playground or simulator
//store the array
let array = ["horse", "cow", "camel", "sheep", "goat"]
let defaults = UserDefaults.standard
defaults.setValue(array, forKey: "history")
defaults.synchronize()
//Retrieve the array
if((UserDefaults.standard.array(forKey: "history")) != nil){
let historyWords = (UserDefaults.standard.array(forKey: "history") as? [String])!
print(historyWords)
}
Store into User Default:
let arrayFruit = ["Apple","Banana","Orange","Grapes","Watermelon"]
//store in user default
UserDefaults.standard.set(arrayFruit, forKey: "arrayFruit")
Retrieving array from User Defaults:
if let arr = UserDefaults.standard.array(forKey: "arrayFruit") as? [String]{
print(arr)
}

NsMutable is not subtype of [anyobject] in swift

I want to get the array from NSUserdefault and store it into NsMutableArray but unfortunately it fails.
error
NsMutable is not subtype of [anyobject] in swift
code
arrayImage = NSUserDefaults.standardUserDefaults().arrayForKey("ImageArray") as NSMutableArray
I am also trying to do downcast NSArray->NSString like in this post.
The NSUserDefaults arrayForKey method returns an optional array of AnyObject. It you want it to be mutable just declare it using "var".// example:
var mutableArray = NSUserDefaults.standardUserDefaults().arrayForKey("ImageArray")
If you want it to be immutable you have to declare it using "let".
let imutableArray = NSUserDefaults.standardUserDefaults().arrayForKey("ImageArray")
If you want to make sure your method does not return nil before setting any value to userdefaults you can use the nil coalescing operator "??" to return an empty array as follow:
var mutableArray = NSUserDefaults.standardUserDefaults().arrayForKey("ImageArray") ?? []
If you want to store images using user defaults you have to first convert your image to NSData. So you should create a method class to help you with it:
class Save {
class func image(key:String, _ value:UIImage) {
NSUserDefaults.standardUserDefaults().setObject(UIImagePNGRepresentation(value), forKey: key)
}
class func imageArray(key:String, _ value:[UIImage]) {
NSUserDefaults.standardUserDefaults().setObject(NSKeyedArchiver.archivedDataWithRootObject(value), forKey: key)
}
}
class Load {
class func image(key:String) -> UIImage! {
return UIImage(data: ( NSUserDefaults.standardUserDefaults().objectForKey(key) as NSData))!
}
class func imageArray(key:String) -> [UIImage]! {
if let myLoadedData = NSUserDefaults.standardUserDefaults().dataForKey(key) {
return (NSKeyedUnarchiver.unarchiveObjectWithData(myLoadedData) as? [UIImage]) ?? []
}
return []
}
}
Testing
let myProfilePicture1 = UIImage(data: NSData(contentsOfURL: NSURL(string: "http://i.stack.imgur.com/Xs4RX.jpg")!)!)!
let myProfilePicture2 = UIImage(data: NSData(contentsOfURL: NSURL(string: "https://lh6.googleusercontent.com/-Axxzrunya9Y/AAAAAAAAAAI/AAAAAAAAAHo/gsDxk5FlliE/photo.jpg")!)!)!
Save.image("myPic", myProfilePicture1)
let myLoadedPic = Load.image("myPic")
let myImageArray = [myProfilePicture1,myProfilePicture2]
Save.imageArray("myPicArray", myImageArray)
let myLoadedPicArray = Load.imageArray("myPicArray")
Apple doc says:
You can also create an NSArray object directly from a Swift array literal, following the same bridging rules outlined above. When you explicitly type a constant or variable as an NSArray object and assign it an array literal, Swift creates an NSArray object instead of a Swift array.
A brief example here:
let imageArray: [AnyObject]? = NSUserDefaults.standardUserDefaults().arrayForKey("ImageArray")
if let array = imageArray{
var arrayImage: NSArray = array
}

Swift NSUserDefaults NSArray using objectForKey

I'm pretty new to Swift, and I've managed to get pretty stuck.
I'm trying to retrieve data from NSUserDefaults and store it in an array (tasks):
#lazy var tasks: NSArray = {
let def = NSUserDefaults.standardUserDefaults()
let obj: AnyObject? = def.objectForKey("tasks")
return obj as NSArray
}()
All I'm getting is a warning: EXE_BAD_INSTRUCTION on line 3.
Also to note that I haven't actually set any data yet, but what I'm aiming for is that if there is no data, I want the array to be empty. I'll be using the data to populate a table view.
Now using a var instead of a constant:
#lazy var tasks: NSArray = {
let def = NSUserDefaults.standardUserDefaults()
var obj: AnyObject? = {
return def.objectForKey("tasks")
}()
return obj as NSArray
}()
The error has now moved to the return line.
I think the problem here is that you are attempting to cast nil to a non-optional type and return it. Swift does not allow that. The best way to solve this would be the following:
#lazy tasks: NSArray = {
let defaults = NSUserDefaults.standardUserDefaults()
if let array = defaults.arrayForKey("tasks") as? NSArray {
return array
}
return NSArray()
}
Using Swift's if let syntax combined with the as? operator lets you assign and safe cast in one line. Since your method does not return an optional, you must return a valid value if that cast fails.

Resources