arrayWithContentsOfFile can't read plist file
Asked Answered
M

5

6

i have a plist with an array that contain 2 strings see image :

http://imageshack.us/photo/my-images/263/myplist.png/

and in my viewDidLoad i add this :

NSString *file = [[NSBundle  mainBundle] pathForResource:@"Data"    ofType:@"plist"];

NSArray *array = [NSArray    arrayWithContentsOfFile:file];

NSLog(@"array = %@", array);

But i got always null why? Thanks for help.

UPDATE : the solution is that you need to manually edit the plist file (Xcode4 use Dictionnary by default)

<?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">
<plist version="1.0">
<array>
    <array>
        <string>HO</string>
        <string>HI</string>
    </array>
</array>
</plist>
Millymilman answered 2/6, 2011 at 4:45 Comment(5)
@Millymilman Did you check that the path exists? Maybe stick a [[NSFileManager defaultManager] fileExistsAtPath:file] in there?Longlimbed
@Aravindhanarvi This is my full code :).Millymilman
@Jacob : i checked and the path exists.Millymilman
@funnyCoder:change the name of the root Key From Array TO Root It is Working finePotentiality
@nikhil : I changed it to root but doesn't work!!Millymilman
S
13

Most likely, your plist is a dictionary that contains an array (Xcode 4 doesn't allow to create plists with an array as the root object). Try something like this:

NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:file];
NSArray *array = [dict objectForKey:@"Array"];
NSLog(@"%@", array);
Soundboard answered 2/6, 2011 at 4:50 Comment(4)
@Soundboard dude it allow to create actually allow to create array as a rootPotentiality
I will give you a good answer :), because it's ok, but the correct answer is that you need to edit by hand te plist file.Millymilman
@nikhil You can do it in Xcode 3 (as the screenshot in your answer shows), but not in Xcode 4, which is shown in funnyCoder's screenshot. I found it hard to believe myself.Soundboard
You can edit the Dictionary plist in TextEdit (or elsewhere), remove the <dict>, </dict> and key, then reopen the file in Xcode and edit it like normal. Then when you load it, it will succeed.Rat
P
0

plist should start with Root Key

in you application you need to change the root key name as it can not be datatype

Potentiality answered 2/6, 2011 at 4:58 Comment(2)
I changed it but no success, it seems that Xcode 4 doesn't allow array plist?!Millymilman
So hiw to deal with that? you can give me an URL about Xcode4 Array plist support? thanks.Millymilman
S
0

I've been studied this topic and discover some differences in plist file.

For example , I have the original plist file in sample code that is read with the method
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];

the xml original file is :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"  
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<array>
<dict>
    <key>nameKey</key>
    <string>Number One</string>
    <key>imageKey</key>
    <string>small_one.png</string>
</dict>
<dict>
    <key>nameKey</key>
    <string>Number Two</string>
    <key>imageKey</key>
    <string>small_two.png</string>
</dict>

</array>        
</plist>

When I tried to create this same file above in Xcode Interface, using Add new File Property List, and after editing puting the item the same way is show in interface for the file above, the method [[NSArray alloc] initWithContentsOfFile:path]; fail, and I found the issue at structure of xml plist file. The file created using XCode Interface, result in xml plist file :

<plist version="1.0">
<dict>
<key>item 0</key>
<dict>
    <key>imageKey</key>
    <string>image1.jpg</string>
</dict>
<key>item 1</key>
<dict>
    <key>imageKey</key>
    <string>image2.jpg</string>
</dict>
</dict>
</plist>

I fixed change direct in xml file as same the first example, and the method works fine.

The point is in XCode interface for plist file, you cant see these differences. The both look the same. Maybe is my Xcode 4.1

Slesvig answered 11/11, 2011 at 14:53 Comment(0)
D
0

The correct answer is, the root item IS already an array (in XCode4.. not sure about the others). So if you put another array as neil d showed in his screen shot, you will end up with an array that contains an array with the two string values.

Deprecatory answered 29/2, 2012 at 3:51 Comment(0)
C
0

plist of this type:

enter image description here

OR (basically both are same)

enter image description here

Can be read like this:

NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"];
_arrFeats = [NSArray arrayWithContentsOfFile:file];
NSLog(@"%i", _arrFeats.count);

Where appFeatures is name of the plist. (iOS 6.1)

Carmen answered 12/3, 2013 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.