How do I set an array of arrays in NSUserDefaults? - ios

I want to set an array of arrays with NSUserDefaults like this:
let array = [["Mexico","USA","Canada"],["Mexico City","Washington","Ottawa"]]
NSUserDefaults.standardUserDefaults().setObject(array, forKey:"Countries")
Then I try to access it
var recalledArray = NSUserDefaults.standardUserDefaults().objectForKey("Countries") as! NSArray
println(recalledArray)
But this is what the console prints:
(
"\U00bfMexico",
"\U00bfUSA",
"Canada"
)
I only get the first array, missing the second one:
["Mexico City","Washington","Ottawa"]
Why am I only getting back the first array?
How do I get back both the arrays?

Im so sorry there was another problem, I was checking if the app was first launched then save the NSUserDefaults if not then dont save it again so i added the second array after I runned the app on the simulator so the code was never saved to NSUserDefaults, but anyway thank you for trying to help

Related

Swift iOS check if a file has been downloaded from json

I have an app that displays and plays a list of podcasts that is fetched from a json file, I would like to add a download feature but to do this I would like to only show a download icon if the podcast has not been downloaded already, is there a way that I can save something like the id element as well as the podcast title so I could then check to see if its been downloaded and saved on the phone already? Or is there an easier way? Obviously I would like to do this before the list is shown.
You can use UserDefaults for that.
Here's an example on how to read an array from UserDefaults
// Retrieves an array of strings from disk
func getDowloadedPodcasts() -> [String] {
UserDefaults.standard.array(forKey: "downloadedPodcasts") as? [String] ?? []
}
And here's an example on how to append a new value to an array on UserDefaults
func addDownloadedPodcast(podcastId: String) {
let downloadedPodcasts = getDowloadedPodcasts()
downloadedPodcasts.append(podcastId)
UserDefaults.standard.setValue(podcastId, forKey: "downloadedPodcasts")
}
Note that this functions alone won't solve your problem nor are the best solution of your problem, they are here jsut to show how easy it can be to work with UserDefaults and to read/write from non-volatile memory

Swift 3 Migration Cleared NSUserDefault Data

I'm not sure how to use the new UserDefaults class with the new Swift3 changes.
I had this code prior to the migration to swift3 which successfully retrieved the data stored in the userDefaults:
if NSUserDefaults.standardUserDefaults().objectForKey("profileArray") != nil {
profileArray = NSUserDefaults.standardUserDefaults().objectForKey("profileArray") as! [String]
}
With the migration to swift3 the code is now:
if UserDefaults.standard.object(forKey: "profileArray") != nil {
profileArray = UserDefaults.standard.object(forKey: "profileArray")! as! [NSString]
}
The new syntax makes sense but when I run the project the data that was previously stored in the user default seems to be gone.The userdefault.standard... is now returning empty/nil.
How do I retrieve the data that was stored prior to the swift3 migration?
Appreciate the help with moving to swift3!
I don't know if that solves your problem, but your syntax is not very efficient – you retrieve the object from user defaults unnecessarily twice.
The recommended way is optional binding and the dedicated method to get an array:
if let array = UserDefaults.standard.array(forKey: "profileArray") as? [String] {
profileArray = array
}
Im Swift use always String rather than NSString unless you have no choice.
I have finally figured this out. The userDefaults weren't "cleared". but the userDefaults are specific to a device. so with the migration to xcode8 the iOS simulators were also upgraded....and these are technically new devices where userDefaults had never been captured.
I proved this theory by adding a simulator (you can go back and re-install iOS 9.x simulators) and this worked.
I also tested on a real device using iOS 9.x & the new swift 3 code and the defaults persisted on that device.
So problem solved! :)

NSString from NSString to set as key in NSMutableDictionary

I am getting a initWithObjects:count:]: attempt to insert nil object from objects[0] at the following line:
[contentsOfCocktails setObject:[NSMutableArray arrayWithObject: recipeTitleA] forKey:cocktailsTitleA];
recipeTitleA is the string I'm creating from the cocktails.recipeID class property that equals A. However, I am getting a recipeTitleA equals nil in the debug window.
Here is where I set cocktails.recipeID equal to recipeTitleA:
if ([cocktails.recipeID isEqualToString:#"A"]){
recipeTitleA = cocktails.recipeID;
}
Is this the correct way to set a string equal to another string in order to use it as a key in a NSMutableDictionary?
Long story short: I am trying to extract the recipeIDs that equal A and set them as a key in a dictionary. I will be doing this with other letters as keys as well. I was then going to store them in an array in which I could create sections in the tableview. Data is brought in with FMDB.
I'm new to obj-c and new to data formatting with arrays and dictionaries. Any help would be greatly appreciated!
It looks like you are doing the string comparison correctly. Set a breakpoint at if ([cocktails.recipeID isEqualToString:#"A"]){ and see what you are getting for cocktails.recipeID, if it's nil go back into your cocktails.recipeID property and look if its being set correctly.

Adding objects from array to view later on in NSTableView?

I'm totally stumped here I have no idea whats going on below is the code I wrote to just go through the array and print out all the other array values within the property list:
for (userFoodDataArray in userFoodDataArray){
NSLog(#"%#", userFoodDataArray);
}
What I'm trying to do "I THINK" is add them as objects so later on I can view them through a search or maybe an NSTableView could someone please head me in the right direction for what I need to do.
Also when i use that NSLog statement the format of how the print out is odd see below:
2014-01-23 21:45:40.212 Carbies[4425:70b] (
Coconut
)
2014-01-23 21:45:40.212 Carbies[4425:70b] (
Watermelon
)
2014-01-23 21:45:40.212 Carbies[4425:70b] (
Egg
)
they have these weird brackets around them why?
This is not into the real object, just NSLog way to print the NSArray object collection.
When you get your object back into NSString or on any label this will be only "String" not any bracket.
It is considering each object as an array. so you can use [userFoodDataArray firstObject], it will return the first object. In your case the only onject it has.

NSNumber in NSDictionary is always null

I'm building a project and want to add a NSNumber into a NSDictionary. But the it crashed because of the null value. So, I created another small program to check what happened. As you can see in the snapshot: Why the the value of NSNumber in NSDictionary is null?
I've run your code and I could reproduce the problem. But it seems like a debugger problem. For instance, if after your dictionary is created, go to the console and try printing the dictionary.
po dictionary
My result is like:
$4 = 0x2083e770 {
number = 1;
}
So it's not null at all. Also, after that, anum is assigned correctly and b is set to YES. So it really looks like a debugger issue instead of a bug from you.

Resources