IPhone JSON to TableView

2019-08-23 15:37发布

问题:

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.

回答1:

cell.textLabel.text = (NSString *)[self.jsonArray objectAtIndex:indexPath.row];
(EDIT: Note that the original code was accessing cell.text rather than cell.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 dictionaries
3. 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).



回答2:

The code you've posted has nothing to do with your crash. Just find all the isEqualToString: in your code using Xcode's search navigator and place breakpoints there. When you find an object that causes this crash find out why it becomes NSDictionary instead of NSString (probably because you're assigning it a wrong value).



回答3:

You will get a NSDictionary back from the JSONValue. Which means that you need to fetch the dictionary and fetch each value you want to display in your table.

NSDictionary *dictionary = [self.jsonArray objectAtIndex:indexPath.row]; cell.textLabel.text = [dictionary objectForKey:@"agencyname"];