2011-12-25 48 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"];