我正在使用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中:
NSLog(@"%@",arr[0]);
存在一些问题。我想打印数组a
和b
的第一个索引的值。
答案 0 :(得分:1)
此处[dic allKeys]
将成为dic
中NSString
喜欢" 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]);
}