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>
funnyCoder
  • 787
  • 2
  • 10
  • 30

5 Answers5

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);
brianegge
  • 29,240
  • 13
  • 74
  • 99
omz
  • 53,243
  • 5
  • 129
  • 141
  • @omz dude it allow to create actually allow to create array as a root – NIKHIL Jun 02 '11 at 04:59
  • 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. – funnyCoder Jun 02 '11 at 05:21
  • 1
    @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. – omz Jun 02 '11 at 15:56
  • You can edit the Dictionary plist in TextEdit (or elsewhere), remove the , and key, then reopen the file in Xcode and edit it like normal. Then when you load it, it will succeed. – bshirley Oct 28 '11 at 22:20
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)

Community
  • 1
  • 1
Vaibhav Saran
  • 12,848
  • 3
  • 65
  • 75
0

plist should start with Root Key

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

NIKHIL
  • 2,719
  • 1
  • 26
  • 50
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

Felipe FMMobile
  • 1,641
  • 20
  • 17
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.

Just a coder
  • 15,480
  • 16
  • 85
  • 138