【问题标题】:Fetching data from `plist`从`plist`中获取数据
【发布时间】:2015-07-16 06:03:18
【问题描述】:

我正在使用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"); 之后的行正在创建一些异常。

【问题讨论】:

    标签: ios objective-c plist


    【解决方案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);
    }
    

    【讨论】:

    • 您的代码运行良好。这里allValues 将其转换为数组。这意味着我们不能在dictionary 内运行for in 循环dictionary,或者我的错误代码还有其他原因。 @Akhilrajtr
    • 是的,字典不能在 for in 循环中使用,因为字典不支持 objectAtIndexedSubscript where array 。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      试试这个:

      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' 等太令人困惑。

      【讨论】:

        猜你喜欢
        • 2012-04-06
        • 1970-01-01
        • 1970-01-01
        • 2013-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多