通过快速枚举从PLIST获取数据

时间:2015-07-28 06:31:21

标签: ios objective-c plist for-in-loop

我正在使用for in循环从plist中获取数据。For in循环是正确的,因为它是打印值。但是,突然之间,它显示了异常

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

代码:

   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

NSLog(@"%@",arr[0]);存在一些问题。我想打印数组ab的第一个索引的值。

1 个答案:

答案 0 :(得分:1)

此处[dic allKeys]将成为dicNSString喜欢" a"," b"等

所以对于你的plist,代码应该是,

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]); 
}