林和我的代码,它返回一个错误,指出问题......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在控制台中正确返回。
我不知道NSDictionary JSON和NSArray JSON之间的friggin区别...下面是我的JSON [{“0”:“Test Agency”,“agencyname” :“测试代理”},{“0”:“测试代理”,“代理名称”:“测试代理”}] – savagenoob 2011-12-25 09:15:49
当您解析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
我爱你的东西。一整天试图弄清楚这一点。 – savagenoob 2011-12-25 19:02:06