0

I am using for in loop to fetch data from plist. But it is showing following 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)

Code:

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

But, if I am fetching data one by one then it is showing on debugger area. for in loop is also working as it is printing check. May be the line after NSLog(@"check"); is creating some exceptions.

Bhupesh Kumar
  • 369
  • 2
  • 18

3 Answers3

1

Try this

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);
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Akhilrajtr
  • 5,170
  • 3
  • 19
  • 30
  • Your code is running fine. Here `allValues` is converting it into array. It means we can't run `for in` loop for `dictionary` inside `dictionary` or is there any other reason for my wrong code. @Akhilrajtr – Bhupesh Kumar Jul 16 '15 at 06:18
  • Yes, the dictionary cannot be used inside a for in loop since dictionary don't have support for `objectAtIndexedSubscript` where array does. – Akhilrajtr Jul 16 '15 at 06:31
0

for in on a dictionary is enumerating its keys, not values. See this question for each loop in objective c for accessing NSMutable dictionary

Community
  • 1
  • 1
pronvit
  • 4,169
  • 1
  • 18
  • 27
0

Try this:

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

I had to change your variable names because it was too confusing to work with 'ald', 'awq', 'qqw' , etc.

Frankie
  • 11,508
  • 5
  • 53
  • 60