Frenck
Frenck

Reputation: 6534

How to count items in plist

I got a question how i can count items in my plist file. I tried:

NSString *bundlePathofPlist = [[NSBundle mainBundle]pathForResource:@"Mything" ofType:@"plist"];

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:bundlePathofPlist];

    NSArray *dataFromPlist = [dict valueForKey:@"some"];

    for(int i =0;i<[dataFromPlist count];i++)
    {
        //NSLog(@"%@",[dataFromPlist objectAtIndex:i]);
        [self setTableData:[dataFromPlist count]];

    }

    NSLog(@"%@", tableData);

But on this line an error appears:

    [self setTableData:[dataFromPlist count]];

Implicit conversion of 'NSUInteger' (aka 'unsigned int') to 'NSArray *' is disallowed with ARC

and warning:

Incompatible integer to pointer conversion sending 'NSUInteger' (aka 'unsigned int') to parameter of type 'NSArray *'; 

Upvotes: 1

Views: 781

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

It looks like your setTableData takes an NSArray instance. You need to prepare an array upfront in a loop, and then set it once, like this:

NSMutableArray *data = [NSMutableArray array];
for(int i =0;i<[dataFromPlist count];i++)
{
    //NSLog(@"%@",[dataFromPlist objectAtIndex:i]);
    [data addObject:[NSNumber numberWithInt:[[dataFromPlist objectAtIndex:i] count]]];
}
[self setTableData:data];

This assumes that your setTableData method expects an array of NSNumber instances wrapping ints.

Upvotes: 2

Lily Ballard
Lily Ballard

Reputation: 185731

The problem is you're using [dataFromPlist count] inside your for loop, which makes no sense. You probably meant [dataFromPlist objectAtIndex:i].

Or, more idiomatically,

for (NSArray *elt in dataFromPlist) {
    [self setTableData:elt];
}

Although I do have to wonder why you're calling -setTableData: over and over with different elements.

Upvotes: 0

Related Questions