从 `plist` 中获取数据

Fetching data from `plist`

我正在使用 for in 循环从 plist 中获取数据。但它显示以下 exception:

2015-07-16 11:16:43.597 plistNeha[640:60b] -[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x8d671b0
2015-07-16 11:16:43.721 plistNeha[640:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x8d671b0'
*** First throw call stack:
(
    0   CoreFoundation                      0x017ed1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0156c8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x0188a243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x017dd50b ___forwarding___ + 1019
    4   CoreFoundation                      0x017dd0ee _CF_forwarding_prep_0 + 14
    5   plistNeha                           0x00002b7d -[sdViewController plistbtn:] + 333
    6   libobjc.A.dylib                     0x0157e880 -[NSObject performSelector:withObject:withObject:] + 77
    7   UIKit                               0x0022e3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
    8   UIKit                               0x0022e345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    9   UIKit                               0x0032fbd1 -[UIControl sendAction:to:forEvent:] + 66
    10  UIKit                               0x0032ffc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
    11  UIKit                               0x0032f243 -[UIControl touchesEnded:withEvent:] + 641
    12  UIKit                               0x0026dddd -[UIWindow _sendTouchesForEvent:] + 852
    13  UIKit                               0x0026e9d1 -[UIWindow sendEvent:] + 1117
    14  UIKit                               0x002405f2 -[UIApplication sendEvent:] + 242
    15  UIKit                               0x0022a353 _UIApplicationHandleEventQueue + 11455
    16  CoreFoundation                      0x0177677f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    17  CoreFoundation                      0x0177610b __CFRunLoopDoSources0 + 235
    18  CoreFoundation                      0x017931ae __CFRunLoopRun + 910
    19  CoreFoundation                      0x017929d3 CFRunLoopRunSpecific + 467
    20  CoreFoundation                      0x017927eb CFRunLoopRunInMode + 123
    21  GraphicsServices                    0x037e15ee GSEventRunModal + 192
    22  GraphicsServices                    0x037e142b GSEventRun + 104
    23  UIKit                               0x0022cf9b UIApplicationMain + 1225
    24  plistNeha                           0x00002e1d main + 141
    25  libdyld.dylib                       0x01e34701 start + 1
    26  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

代码:

NSString *path=[[NSBundle mainBundle]pathForResource:@"x" ofType:@"plist"];

ald=[[NSDictionary alloc]initWithContentsOfFile:path];

for(NSDictionary *awq in ald){

     NSLog(@"check");
     NSString *qqw=@"";
     qqw=[awq objectForKey:@"qq"];
     NSLog(@"%@",qqw);
   }

但是,如果我正在一个接一个地获取数据,那么它就会显示在调试器区域中。 for in 循环也在打印 check。可能是 NSLog(@"check"); 之后的行正在创建一些异常。

试试这个

NSString *path=[[NSBundle mainBundle]pathForResource:@"x" ofType:@"plist"];

NSDictionary *ald=[[NSDictionary alloc]initWithContentsOfFile:path];

for(NSDictionary *awq in [ald allValues]){

     NSLog(@"check");
     NSString *qqw=@"";
     qqw=[awq objectForKey:@"qq"];
     NSLog(@"%@",qqw);
}
字典上的

for in 正在枚举其键,而不是值。看到这个问题for each loop in objective c for accessing NSMutable dictionary

试试这个:

NSString *path=[[NSBundle mainBundle]pathForResource:@"x" ofType:@"plist"];
NSDictionary *plistDictionary = [[NSDictionary alloc]initWithContentsOfFile:path];

for(NSString *key in plistDictionary){

    NSDictionary *dictionary = [plistDictionary objectForKey:key];
    NSLog(@"%@",[dictionary objectForKey:@"qq"]);
}

我不得不更改你的变量名,因为使用 'ald'、'awq'、'qqw' 等太混乱了