Im having issues with my code, its returning an error that says…
2011-12-24 22:52:36.280 BusinessManager[479:20b] * Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘* -[NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x3e965e0’>
Here is the code:
#import "BusinessManagerAppDelegate.h"
#import "ProspectViewController.h"
#import "JSON.h"
@implementation ProspectViewController
@synthesize jsonArray;
- (void)viewDidLoad {
NSURL *jsonURL = [NSURL URLWithString:@"https://www.mysite.php"];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];
NSLog(jsonData);
self.jsonArray = [jsonData JSONValue];
[jsonURL release];
[jsonData release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [jsonArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Prospects = @"agencyname";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Prospects];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:Prospects] autorelease];
}
cell.text = (NSString *)[self.jsonArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
}
- (void)viewDidDisappear:(BOOL)animated {
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[jsonArray dealloc];
[super dealloc];
}
@end
Im pretty sure I have everything set up correctly and the JSON is returning correctly in the console.
cell.textLabel.text = (NSString *)[self.jsonArray objectAtIndex:indexPath.row];(EDIT: Note that the original code was accessing
cell.textrather thancell.textLabel.text)This line is likely the error. Let’s look at it step by step:
1. The JSON output is an array, stored in jsonArray (check to make sure it’s not a dictionary too).
2.
[self.jsonArray objectAtIndex:indexPath.row]is likely an NSDictionary. As you can see from the exception that’s returning, it involves a NCSFDictionary. In fact, many times, JSON outputs are arrays of dictionaries3. With the error ‘NSInvalidArgumentException’, reason: ‘* -[NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x3e965e0’>, the code is trying to compare an NSDictionary to an NSString.
4. To solve this, look at the JSON output more carefully and dissect it! And make sure that the JSON output isn’t varying from case to case (with different URLs).