Search code examples
iosobjective-cplistfor-in-loop

Fetching data from PLIST by Fast Enumeration


I am using for in loop to fetch data from plist.For in loop is correct as it is printing value. But,suddenly, it is showing following exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x97a14b0'.

Code:

   NSString *sss=[[NSBundle mainBundle]pathForResource:@"s" ofType:@"plist"];

    NSDictionary *dic=[[NSDictionary alloc]initWithContentsOfFile:sss];

    for(NSArray *arr in [dic allKeys])
    {

    NSLog(@"%@",arr); // Ii is printing value

    NSLog(@"%@",arr[0]); // It is showing exceptions
    }

Plist:

enter image description here

There is some problem with NSLog(@"%@",arr[0]);. I want to print values of first index of arrays a and b.


Solution

  • here [dic allKeys] will be keys in dic that are NSString like "a", "b" etc.

    so for your plist the code should be,

    NSString *sss=[[NSBundle mainBundle]pathForResource:@"s" ofType:@"plist"];
    
    NSDictionary *dic=[[NSDictionary alloc]initWithContentsOfFile:sss];
    
    for(NSString *key in [dic allKeys])
    {
    
         NSLog(@"%@",key); 
         NSArray *value = [dic objectForKey:key];
         NSLog(@"%@",value[0]); 
    }