views:

4218

answers:

3

I am trying to get an array from a plist as suggested on an answer here but it's not working. Can someone tell me what I'm missing?

Here is what the plist looks like....another weird thing is that I can't get this to open in a plain text file?? when I do it looks like garbage.....but it looks fine in the property list editor:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
<plist version="1.0">
<dict>
<key>count</key>
<array>
     <integer>5</integer>
</array>
<key>username</key>
<array>
     <string>johnsmith</string>
</array>
</dict>
</plist>

Here is the code i'm using..... dictionary is a NSMUtableDictionary and accountsArray is an NSMustableArray that are in the .h file.

     NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:accountsFilePath];

 dictionary = tempDictionary;
 [tempDictionary release];


 NSMutableArray *nameArray = [[NSMutableArray alloc] init];
 nameArray = [dictionary objectForKey:@"username"];

 accountsArray = nameArray;

 [nameArray release];

Am I missing something? Screwing something up?

Thanks...

+1  A: 

Well, there are a number of issues afoot; first of all the plist is probably a binary property list, and not actually an XML property list. This doesn't matter much on the code side of things, but should serve to explain the issues with opening as plain text.

Second, by releasing tempDictionary, you are also releasing dictionary, as they both point to the same object. This should serve to explain why your code doesn't work. Try autorelease, or simply dictionaryWithContentsOfFile: and nix the release phrase alltogether. (dictionaryWithContentsOfFile: will autorelease its return value.)

You also repeat the release mistake on the second phrase; the assignment operator (=) doesn't copy in Objective-C; it assigns.

Edit: The latter is not true when using property notation; assuming that properties are defined; self.accountsArray = nameArray would (probably) copy the array into the object.

Reedit: Correct code:

dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:accountsFilePath];
accountsArray = [dictionary objectForKey:@"username"];
Williham Totland
I'm doing this now and now i'm crashing.... dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:accountsFilePath]; if (accountsArray == nil){ accountsArray = [[NSMutableArray alloc]init]; } NSMutableArray *nameArray = [[NSMutableArray alloc] init]; nameArray = [dictionary objectForKey:@"username"]; accountsArray = nameArray; [nameArray release];
Xcoder
A: 

I finally got it working like this:

    dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:accountsFilePath];      


 if (accountsArray == nil){

  accountsArray = [[NSMutableArray alloc]init];
 }

 if (countArray == nil){

  countArray = [[NSMutableArray alloc]init];
 }



 countArray = [dictionary objectForKey:@"count"];
 accountsArray = [dictionary objectForKey:@"username"];
Xcoder
actually, I used this: if (accountsArray == nil){ accountsArray = [[[NSMutableArray alloc]init] autorelease]; } if (countArray == nil){ countArray = [[[NSMutableArray alloc]init] autorelease]; }
Xcoder
A: 

Just use actionscipt

noobiPhoneDev