Reading from plist to NSArray - objective-c

I am trying to read and I also need to write to a plist.
So far I am trying to simply read the contents in.
Here is my appSettings.plist:
NSBundle* bundle = [NSBundle mainBundle];
NSString* plistPath = [bundle pathForResource:#"appSettings" ofType:#"plist"];
NSDictionary *tmp = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
I tried to do this:
NSArray *mruItems = [[NSArray alloc] initWithArray:[tmp objectForKey:#"lastSearches"]];
But it throws an error. A check I did on the [tmp objectForKey:#"lastSearches"] type revealed this is not an NSArray...
How can I read the contents into my NSArray?
Thanks!
(I would love to have some info on writing too)

If you look at the screenshot above you see that it's pretty clear that "lastSearches" is a Dictionary, not a list. You can try to switch the type there to a Array type instead and it should work for you.
Another solution would be to iterate over the keys in that dictionary:
NSDictionary *lastSearches = [tmp objectForKey:#"lastSearches"];
for (NSString *key in lastSearches.allKeys)
{
NSString *value = [lastSearches objectForKey:key];
}
Note that this would not be in order, and you probably would have to sort before iterating over it.

Indeed it's not an array but a dictionary (look at your image).

Related

Accessing a second NSDictionary in the plist?

I was wondering how I could access the second NSDictionary in my plist other then the root dictionary, what I would like is to have a string in the second dictionary to show up in the console right now I'm getting all of them. Here's my code, console and plist.
My Plist. (its name is Multi.plist)
My code.
NSString *pathForPlist = [[NSBundle mainBundle] pathForResource:#"Multi" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:pathForPlist];
NSLog(#"%#", [dict objectForKey:#"Abs"]);
And what my console is spitting out right now.
So after looking at that, I'll explain again what I would like, instead of having all of the contents of the Abs NSDictionary being in the console, I would like just to show the title string which lies within the Dictionary. Any help would be greatly appreciated.
Thanks
The Abs entry is a dictionary itself, so all you have to do is :
NSDictionary * abs = dict[#"Abs"];
NSLog(#"%#", abs[#"title"]);
NSDictionary *absDict = [dict objectForKey:#"Abs"]);
NSString *title = [absDict objectForKey:#"title"]);
NSLog(#"Title: %#", title);

Replace content of NSMutableArray with NSArray

I'm working on an app that needs to retrieve some data from a server. I have created a "Server" class which handles all the communication and has a NSMutableArray *sessionData variable where I would like to store the data coming from the server (btw, is this approach correct?).
I have the data in an NSArray. I would like the NSMutableArray to have the same content of the NSArray but I didn't find any way to do this (sessionData = requestResult).
(subquestion: do I have to initialize in some way the NSMutableArray before using ? I have only declared it with #property and #synthesize)
The code you tried (from the comment) should have worked. The reason it did not work is that your sessionData was nil.
You need to initialize your sessionData - set it to [NSMutableArray array] in the initializer; then your code
[sessionData removeAllObjects];
[sessionData setArray:result];
will work perfectly. You do not even need the first line - the second one replaces the content of sessionData with that of the result.
Try this way:
sessionData = [result mutableCopy];
[result release];
Or
NSMutableArray *sessionData = [[NSMutableArray alloc] initWithContentsOfArray:result];
Or, if you could do this:
NSMutableArray *session = [NSMutableArray arrayWithArray:someArray];
1. is this approach correct?
Yes.
2. I didn't find any way to do this (sessionData = requestResult)
As many have suggested you can use mutableCopy to assign requestResult to sessionData
OR you can use arrayWithArray as one answer suggests.
3. do I have to initialize in some way the NSMutableArray before using ?
Yes. If you are changing any variable it must have memory allocated.
In your example something like this:
NSArray *requestData = [[NSArray alloc] initWithObjects:#"3", #"4", #"5", nil];
_sessionData = [[NSMutableArray alloc] initWithArray:requestData];
[requestData release];
NSLog(#"%#", [sessionData objectAtIndex:0]); // 2012-03-30 15:53:39.446 <app name>[597:f803] 3
NSLog(#"count: %d", [sessionData count]); //2012-03-30 15:53:39.449 <app name>[597:f803] count: 3

Editing a plist in NSDictionary

I'm importing a plist into an NSDictionary object and want to change the contents of the plist once in the dictionary to lowercase. I have tried several different ways with no luck. Maybe I am supposed to edit the strings that are inside the NSDictionary, but then I don't know how to do that.
What I have currently imports the plist correctly, but the lowercaseString line effectively does nothing.
NSString *contents = [[NSBundle mainBundle] pathForResource:#"Assets/test" ofType:#"plist"];
NSString *newContents = [contents lowercaseString];
NSDictionary *someDic = [NSDictionary dictionaryWithContentsOfFile:newContents];
for (NSString *someData in someDic) {
NSLog(#"%# relates to %#", someData, [someDic objectForKey:someData]);
}
I NSLog'd the "content" string and it gave me a path value, so obviously changing that to a lowercaseString wouldn't do anything. I'm feeling like I have to access the strings within the dictionary.
This line
NSString *newContents = [contents lowercaseString];
is change the path string returned from
NSString *contents = [[NSBundle mainBundle] pathForResource:#"Assets/test" ofType:#"plist"];
so you will end up with something like ../assets/test.plist
you will need to walk through the contents of someDic an create a new dictionary based on the old turning strings into lowercase, if you are only concerned about values directly in someDic you can do something like
for( NSString * theKey in someDic )
{
id theObj = [someDic objectForKey:theKey];
if( [theObj isKindOfClass:[NSString class]] )
theObj = [theObj lowercaseString];
[theNewDict setObject:theObj forKey:theKey];
}

What is the best way to store an array of objects with cocoa?

I have an NSMutableArray with many objects in it. Some are NSStrings, others are NSMutableArrays, others are NSNumbers. What is the best way to store this data so that the app could use it again?
The array needs to stay in order as well.
I'm thinking a plist or NSUserDefaults?
Many thanks
Consider using NSKeyedArchiver.
// Archive
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:theArray];
NSString *path = #"/Users/Anne/Desktop/archive.dat";
[data writeToFile:path options:NSDataWritingAtomic error:nil];
// Unarchive
NSString *path = #"/Users/Anne/Desktop/archive.dat";
NSMutableArray *theArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
It works great, especially in case the array contains more then strings and numbers.
This way you can be sure the unarchived array is identical to the original.
Assuming the other arrays also contain only numbers, strings, and arrays (or other property list types), a plist would be a great way to store your data. It will keep it's order, and is simple to use.
To write an array to a plist file, use writeToFile:atomically:.
[myArray writeToFile:#"path/to/file.plist" atomically:YES];
To read it, use initWithContentsOfFile:.
myArray = [[NSMutableArray alloc] initWithContentsOfFile:#"path/to/file.plist"];
However, that will create a mutable array with non-mutable contents. To create an array with mutable contents, you can use CFPropertyListCreateDeepCopy.
NSArray *temp = [[NSArray alloc] initWithContentsOfFile:#"path/to/file.plist"];
myArray = (NSMutableArray*) CFPropertyListCreateDeepCopy(kCFAllocatorDefault,(CFArrayRef)temp,kCFPropertyListMutableContainers);
[temp release];

Getting an NSString out of an NSArray

I am trying to save and read back some application settings stored as NSStrings in an iPhone app and have been having some trouble.
The code to save looks like:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:accountID];
...
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
And the code to read looks like (accountID is an NSString*):
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
accountID = [array objectAtIndex:0];
...
[array release];
NSLog(#"Loading settings for: %#", accountID);
The read code throws an exception because after the array is released the accountID variable also appears to have been released (moving the NSLog call before releasing the array works fine). So I'm guessing that I'm creating a reference to the array instead of pulling out the actual string contained in the array. I tried several things to create new strings using the array contents but haven't had any luck.
You guess is on the right lines although you have a reference to the 0th element of the array not the array. The array consists of pointers to NSString objects. The Strings will get get released when yhe array is released.
You need to retain the element you are using e/g/
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
NSString* accountID = [[array objectAtIndex:0]retain];
...
[array release];
NSLog(#"Loading settings for: %#", accountID);
When you release the array the reference to the accountID will also be released. You need to retain it.
accountID = [[array objectAtIndex:0] retain];
Then obviously at some point you need to release it.
try [accountID retain] before you release the array