How can i transfer array to table in Today Extansion (Swift)
I'm new to this and nothing happens
In main app i have a table with data
In Today Extansion i have a table (in which the data should be displayed)
its work with TEXT but not work with ARRAY
var myData: [String] = []
MainVC.swift
if let userDefaults = UserDefaults(suiteName: "group.Name1") {
userDefaults.set("Test12345" as AnyObject, forKey: "key3")
userDefaults.synchronize()
}
TodayExtVC.swift
In ViewDidLoad:
if let userDefaults = UserDefaults(suiteName: "group.Name1") {
let value2 = userDefaults.string(forKey: "key3")
print("\(value2)")
}
Set in MainVC.swift
let array = ["horse", "cow", "camel", "sheep", "goat"]
userDefaults.set(array, forKey: "key3")
get in TodayExtVC.swift
let myarray = userDefaults.stringArray(forKey: "key3") ?? [String]()
Related
I am trying to pass swift array of dictionaries to the NSMutableArray. But I am getting error "Cannot convert value of type [[String : Anyobject?]] to expected argument type NSMutableArray". Below is the code:
var ary : [[String:AnyObject?]] = []
var mutableDictionary = [String: AnyObject?]()
for c in buffer {
mutableDictionary.updateValue(c.Name, forKey: "name")
mutableDictionary.updateValue(c.Number, forKey: "phoneNumber")
mutableDictionary.updateValue(c.id, forKey: "id")
ary.append(mutableDictionary)
}
Now passing this "ary" to the Objective C method as a NSMutableArray!
Manager.sharedInstance().List(ary)
Replica of your issue:
var ary : [[String:AnyObject]] = []
var mutableDictionary = [String: AnyObject]()
var mutableArray:NSMutableArray!
for _ in 0...4 {
mutableDictionary.updateValue("adsf", forKey: "name")
mutableDictionary.updateValue("dsf", forKey: "phoneNumber")
mutableDictionary.updateValue("sdfd", forKey: "id")
ary.append(mutableDictionary)
}
mutableArray = NSMutableArray(array: ary)
Removing the optional did the trick!
// try like this
Manager.sharedInstance().List(NSMutableArray(array: ary as! NSArray))
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)
}
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…
}
}
So I want to append an array in the NSUserDefaults, not save an entirely new one. The issue is that I want the array to be a logged history of a certain type of user interaction so I need to be appending an existing array in the NSUserDefaults, and not overwriting it.
i.e Not:
var data = [String]()
var questionsAsked = [String]()
func storeData() {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(questionsAsked, forKey: "questionsAsked")
defaults.setObject(data, forKey: "data")
var storedQuestionsAsked = defaults.objectForKey("questionsAsked") as? [String] ?? [String]()
var storedData = defaults.objectForKey("data") as? [String] ?? [String]()
}
The array(s) are based in my Modal, and the appending is occurring by appending that array in the ViewController of that Modal
I tried just calling a function like this, but this is just calling an empty array because its never being re-saved to the NSUserDefaults:
func updateQuestionsAsked(questionAsked: String, answerGiven: String) {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject([String](), forKey: "questionsAsked")
defaults.setObject([String](), forKey: "data")
var storedQuestionsAsked = defaults.objectForKey("questionsAsked") as? [String] ?? [String]()
var storedData = defaults.objectForKey("data") as? [String] ?? [String]()
storedData.append(questionAsked)
storedQuestionsAsked.append(answerGiven)
NSUserDefaults.standardUserDefaults().synchronize()
}
Is there some kind of synchronization thing I'm missing? I'm sure this is super simple but I'm just spacing out on what to do. Thanks!
YOu are setting empty data for keys "questionsAsked" & "data",
func updateQuestionsAsked(questionAsked: String, answerGiven: String) {
let defaults = NSUserDefaults.standardUserDefaults()
//remove this
//defaults.setObject([String](), forKey: "questionsAsked")
//defaults.setObject([String](), forKey: "data")
var storedQuestionsAsked = defaults.objectForKey("questionsAsked") as? [String] ?? [String]()
var storedData = defaults.objectForKey("data") as? [String] ?? [String]()
storedData.append(questionAsked)
storedQuestionsAsked.append(answerGiven)
// then update whats in the `NSUserDefault`
defaults.setObject(questionsAsked, forKey: "questionsAsked")
defaults.setObject(data, forKey: "data")
// call this after you update
defaults.synchronize()
}
Another is maybe you failed to set object here...
func storeData() {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(questionsAsked, forKey: "questionsAsked")
defaults.setObject(data, forKey: "data")
// call this after setting Objects
//--
defaults.synchronize()
//--
...
}
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)
}