从'plist`中获取数据

时间:2015-07-16 06:03:19

标签: ios objective-c plist

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

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");创建一些例外后的行。

3 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:0)

字典上的

for in枚举其键,而不是值。请参阅此问题for each loop in objective c for accessing NSMutable dictionary

答案 2 :(得分:0)

试试这个:

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

我不得不更改您的变量名称,因为它太混乱了,无法使用' alq'' awq',' qqw'等等。