-1

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???

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Francis F
  • 3,157
  • 3
  • 41
  • 79

3 Answers3

3

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.
icodebuster
  • 8,890
  • 7
  • 62
  • 65
0

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.

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
  • Tried allocating alloc NSMutableArray, but didnt work, menuArray.count is still returning 0 objects. I've checked one of my old programs and the same code works fine there, that was done without using ARC, this project uses ARC, would it make any difference, if so what changes should I make? – Francis F Jul 30 '13 at 10:52
0

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"];
Mikhail
  • 4,271
  • 3
  • 27
  • 39