showing elements of dictionary

1

I have a plist file that contains array of dictionaries, and I'm trying to show in console the elements of the dictionnaries... here is my code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"validrep" ofType:@"plist"];
descArray = [[NSMutableArray alloc] init];

NSString *key = [NSString stringWithFormat:@"test %i",i+1];

NSMutableArray *tabreponses = [[NSMutableArray arrayWithContentsOfFile:path] retain];

NSDictionary *dictreponses = [NSDictionary dictionaryWithObject:[tabreponses objectAtIndex:i] forKey:key];

[descArray addObject:dictreponses];

NSDictionary *dictionary = [descArray objectAtIndex:i];
NSArray *array = [dictionary objectForKey:key];


NSLog(@"the array is %@, it contains %i elements.",array,[array count]);

Till now everythig is working correctly and this is what the console is showing:

2011-12-14 14:52:33.845 Daltonien[1459:b603] the array is {
    rep1 = "1 \U00e9toile derri\U00e8re un quadrillage ";
    rep2 = "1 lettre B derri\U00e8re un quadrillage";
    rep3 = "1 quadrillage seul ";
}, it contains 3 elements.

but it doesn't work when i try to show the first element of the array, i do it like this:

NSLog(@"the array is %@, it contains %i elements, the first element is %@ .",array,[array count],[array objectAtIndex:i]);


This results in an exception:

Daltonien[1478:b603] -[__NSCFDictionary objectAtIndex:]: 
    unrecognized selector sent to instance 0x4b8cbd0
Daltonien[1478:b603] *** Terminating app due to 
    uncaught exception 'NSInvalidArgumentException', 
      reason: '-[__NSCFDictionary objectAtIndex:]: 
        unrecognized selector sent to instance 0x4b8cbd0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00dca5a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f1e313 objc_exception_throw + 44
    2   CoreFoundation                      0x00dcc0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00d3b966 ___forwarding___ + 966
    4   CoreFoundation                      0x00d3b522 _CF_forwarding_prep_0 + 50
    5   Daltonien                           0x00002cff -[DetailViewController tableView:numberOfRowsInSection:] + 193
    6   UIKit                               0x004772b7 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 1834
    7   UIKit                               0x00474d88 -[UITableViewRowData numberOfRows] + 108
    8   UIKit                               0x00328677 -[UITableView noteNumberOfRowsChanged] + 132
    9   UIKit                               0x00335708 -[UITableView reloadData] + 773
    10  UIKit                               0x00332844 -[UITableView layoutSubviews] + 42
    11  QuartzCore                          0x01d6aa5a -[CALayer layoutSublayers] + 181
    12  QuartzCore                          0x01d6cddc CALayerLayoutIfNeeded + 220
    13  QuartzCore                          0x01d120b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
    14  QuartzCore                          0x01d13294 _ZN2CA11Transaction6commitEv + 292
    15  QuartzCore                          0x01d1346d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
    16  CoreFoundation                      0x00dab89b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
    17  CoreFoundation                      0x00d406e7 __CFRunLoopDoObservers + 295
    18  CoreFoundation                      0x00d091d7 __CFRunLoopRun + 1575
    19  CoreFoundation                      0x00d08840 CFRunLoopRunSpecific + 208
    20  CoreFoundation                      0x00d08761 CFRunLoopRunInMode + 97
    21  GraphicsServices                    0x017211c4 GSEventRunModal + 217
    22  GraphicsServices                    0x01721289 GSEventRun + 115
    23  UIKit                               0x002c8c93 UIApplicationMain + 1160
    24  Daltonien                           0x000020c8 main + 102
    25  Daltonien                           0x00002059 start + 53
    26  ???                                 0x00000001 0x0 + 1
)
terminate called throwing an exception

with what should i change objectAtIndex:i

THANX for helping me.

objective-c
nsdictionary
asked on Stack Overflow Dec 14, 2011 by Hamdi-Lachaal • edited Dec 14, 2011 by (unknown user)

2 Answers

4

The problem is this:

Your key, @"test1", is actually a dictionary, not an array... To get the first key / value of the dictionary, you would do the following

[[myDictionary keys] objectAtIndex:0] 
[[myDictionary values] objectAtIndex:0]
answered on Stack Overflow Dec 14, 2011 by Richard J. Ross III
0

If you want the first object in your array, you'll want something like this:

[array objectAtIndex:0];

To loop through all of the objects, this will do it:

for (id someObject in array) {
   // Do stuff
}
answered on Stack Overflow Dec 14, 2011 by Greg

User contributions licensed under CC BY-SA 3.0