DShah
DShah

Reputation: 9866

plist in Xcode creation

When I am creating a simple application for plist shown in below link:

http://iosdevelopertips.com/data-file-management/reading-a-plist-into-an-nsarray.html

When I am debugging it.. I am getting path of my plist file. But when I am using the following statement

// Build the array from the plist  
NSMutableArray *array2 = [[NSMutableArray alloc] initWithContentsOfFile:path];

I don't get any value in array2... What could be the problem?

Upvotes: 3

Views: 1362

Answers (4)

SmileBot
SmileBot

Reputation: 19642

In xcode 5 > make sure the file is in your compile sources area. enter image description here

Upvotes: 0

Kamil
Kamil

Reputation: 1

I think you could try something like this:

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    NSMutableArray *array = [dict objectForKey:@"Root"];

    // Show the string values  
    for (NSString *str in array)
    {
        NSLog(@"--%@", str);
    }

If that helped, please, drop a line. :)

Cheers, Kamil

Upvotes: 0

DShah
DShah

Reputation: 9866

Here is the final solution that i got for plist....

When u are creating your plist file with first element as Array.. then its XML Contents will be as below:

<?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">
<dict>
    <key>Root</key>
    <array>
        <string>Firecracker</string>
        <string>Lemon Drop</string>
        <string>Mojito</string>
    </array>
</dict>
</plist>

Here you are having unnecessary tag instead of which we should have as your root element must be Array... because u are taking in NSMutableArray object...

so your plist file must be like this

<?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>
        <string>Firecracker</string>
        <string>Lemon Drop</string>
        <string>Mojito</string>
    </array>
</plist>

i have checked that its working fine....

Upvotes: 5

user756245
user756245

Reputation:

The problem could be

  1. The file doesn't exist at specified path, you get nil in array2.
  2. The file exists but it cannot be read correctly, due to some format corruption, and you get nil in array2 too.
  3. The file exists and its format is correct, but there are no values in, you get an empty array.

When using NSString *path = [[NSBundle mainBundle] pathForResource:@"DrinkArray" ofType:@"plist"];, it assumes you have a correctly formed plist file named "DrinkArray" ('DrinkArray.plist'), inside your app bundle. Make sure your file is also copied into the app bundle (check this in Xcode, click on your project, then Build phases, your file should appear in the 'Copy Bundle Ressources').

I guess you get nilas the path to the file, and therefore in array2. Try to log your path string to check it.

Upvotes: 1

Related Questions