Question

I am trying to populate my array using Plist. This is my plist file,Menu.plist.

-Item 0, type: dictionary, value(1 item) - Title, string, Contacts

-Item 1, type: dictionary, value(1 item) - Title, string, Edit

  - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];
        self.menuArray = [NSMutableArray arrayWithContentsOfFile:plistPath];


    }

I'm trying to populate my table view with cell Contacts and Edit but my menuArray is not getting loaded, I checked with NSLog and *plist is getting the path, but in self.menuArray its still shows 0 objects while it should be 2. I have done this before and it has worked fine I dont know what has gone wrong today. Any suggestions???

Was it helpful?

Solution

As per your question the plist content is as follows

Plist created through XCode

XML Format

<?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>
    <dict>
        <key>Title</key>
        <string>Contact</string>
    </dict>
    <dict>
        <key>Title</key>
        <string>Edit</string>
    </dict>
</array>
</plist>

If this is the case try the following

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];
NSArray *menuDataArray = [NSMutableArray arrayWithContentsOfFile:plistPath];

NSLog(@"Plist Content Array = %@", menuDataArray);

Output

Plist Content Array = (
        {
        Title = Contact;
    },
        {
        Title = Edit;
    }
)

Note:

  • Please check your Plist structure.
  • Array alloc-init.

OTHER TIPS

Try this

- (void)viewDidLoad
    {
        [super viewDidLoad];
        self.menuArray=[[NSMutableArray alloc]init];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];
        self.menuArray = [NSMutableArray arrayWithContentsOfFile:plistPath];


    }

you have to alloc NSMutableArray.

Can you paste content of your plist file? If you want to create array from plist, then it should be something 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>Brand A</string>
    <string>Brand B</string>
    <string>Brand C</string>
    <string>Brand D</string>
</array>
</plist>

In your case you probably try to create NSArray from Dictionary-based plist. And you should use:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];

If some object of that dictionary is array, you can get it via:

NSArray *array = [dict objectForKey:@"KeyForSomeArray"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top