2011-12-25 50 views
0

林和我的代码,它返回一个错误,指出问题......IPhone JSON来的TableView

2011-12-24 22:52:36.280 BusinessManager [479:20B] *终止由于应用未捕获的异常 'NSInvalidArgumentException',原因: '* - [NSCFDictionary isEqualToString:]:无法识别的选择发送到实例0x3e965e0'>

下面是代码:

#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 

林相当肯定我有一切正确设置和JSON在控制台中正确返回。

回答

2

cell.textLabel.text = (NSString *)[self.jsonArray objectAtIndex:indexPath.row];
(编辑:注意原代码访问cell.text而非cell.textLabel.text

这条线是有可能的错误。让我们一步一步来看看:
1. JSON输出是一个数组,存储在jsonArray中(检查以确保它不是字典)。
2. [self.jsonArray objectAtIndex:indexPath.row]可能是一个NSDictionary。正如您从返回的异常中可以看到的那样,它涉及到一个NCSFDictionary。事实上,多次,JSON输出是字典
3.用错误'NSInvalidArgumentException' 的阵列,原因: '* - [NSCFDictionary isEqualToString:]:无法识别的选择发送到实例0x3e965e0'>,代码试图将一个NSDictionary与一个NSString进行比较。
4.要解决这个问题,请仔细查看JSON输出并仔细分析!并确保JSON输出不因案例而异(使用不同的URL)。

+0

我不知道NSDictionary JSON和NSArray JSON之间的friggin区别...下面是我的JSON [{“0”:“Test Agency”,“agencyname” :“测试代理”},{“0”:“测试代理”,“代理名称”:“测试代理”}] – savagenoob 2011-12-25 09:15:49

+0

当您解析JSON时,您将它转换为Objective-C。这可以作为一个NSDictionary或一个NSArray来。你可以在这里了解更多关于NSDictionaries的内容:http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html。基本上,你会想把它改成'NSDictionary * infoDictionary = [self.jsonArray objectAtIndex:indexPath.row]'。然后执行'cell.text = [infoDictionary objectForKey:@“agencyname”]'。一个NSDictionary是一种存储具有“关键”和“值”(“机构名称” - >“测试代理” – Louis 2011-12-25 10:17:48

+0

我爱你的东西。一整天试图弄清楚这一点。 – savagenoob 2011-12-25 19:02:06

0

您发布的代码与您的崩溃无关。只需找到所有的isEqualToString:在您的代码中使用Xcode的搜索导航器并在那里放置断点。当你发现一个导致这次崩溃的对象时,找出它为什么变成NSDictionary而不是NSString(可能是因为你给它赋了一个错误的值)。

+0

isEqualToString是在JSON类,所以我觉得这是在代码的东西有问题。 – savagenoob 2011-12-25 09:15:03

+0

然后你可能传递解析NSDictionary而不是NSString – Max 2011-12-25 13:34:04

0

您将从JSONValue返回一个NSDictionary。这意味着您需要获取字典并获取想要在表格中显示的每个值。

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