林和我的代碼,它返回一個錯誤,指出問題......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