I have a problem with my code and i can't figure out what's wrong.
My code is this :
@implementation FirstViewController {
NSDictionary *countriesDetails;
NSArray *countriesList;
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"countries" withExtension:@"plist"];
countriesDetails = [NSDictionary dictionaryWithContentsOfURL:url];
countriesList = countriesDetails.allKeys;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return countriesDetails.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text = [[countriesList objectAtIndex:indexPath.row] objectForKey:@"country"];
return cell;
}
My plist file is this :
<dict>
<key>Paris</key>
<dict>
<key>country</key>
<string>France</string>
<key>chName</key>
<string>Paris</string>
<key>chlink</key>
<string>www.paris.com</string>
</dict>
<key>Rome</key>
<dict>
<key>country</key>
<string>Italy</string>
<key>chName</key>
<string>Rome</string>
<key>chlink</key>
<string>www.rome.com</string>
</dict>
So my app will have multiple tableviews, and the first one shows all the countries I have on my plist.
My XCode shows no error, but when I am running the app it crashes and this appears:
2015-05-26 23:34:53.362 newApp[1511:95592] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fde31f242d0 2015-05-26 23:34:53.366 neaApp[1511:95592] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fde31f242d0' * First throw call stack:
Your countriesList
consists of NSString
s, not NSDictionary
s.
Change your tableView:cellForRowAtIndexPath:
method to:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text = [[countriesDetails objectForKey:[countriesList objectAtIndex:indexPath.row]] objectForKey:@"country"];
return cell;
}