2013-02-18 73 views
0

程序在下面的註釋行崩潰。不知道有什麼問題。第一次程序員的任何提示?ObjectAtIndex崩潰

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"]; 

    if(cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"]; 
    } 

    cell.textLabel.text = [[users objectAtIndex:indexPath.row] objectForKey:[[NSString alloc] initWithFormat:@"%d", indexPath.row]]; 
    //My program runs but then crashes at this line, I have no idea what's wrong with it though "reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance" 


    return cell; 
} 

更多的代碼(不是所有的它雖然)的要求

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

    users = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil]; 
    [self.tableView reloadData]; 
} 

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [users count]; 
} 

在.h文件中

@interface GIFUsersListViewController : UITableViewController {

NSArray *users; 
NSMutableData *data; 

} @end

+0

雖然你可能*認爲*用戶是Array的編譯器錯誤是告訴你它不是。編譯器是正確的。 – MarkPowell 2013-02-18 00:25:09

+0

用戶顯然實際上是一個NSDictionary,而不是你所期望的。您應該在用戶創建的地方發佈代碼。 – 2013-02-18 00:26:11

+0

好的。我會發布更多的代碼,所以看起來像這個部分是好的 - 因爲這是我得到一個綠色的S​​IGARBT標誌 – GangstaGraham 2013-02-18 00:27:44

回答

1

隨着此消息的:'-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance它看起來像用戶不是NSArray,而是NSDictionary


如果你不知道什麼是JSON,然後分配給用戶前檢查:

id usersFromJSON = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil]; 
if ([usersFromJSON isKindOfClass:[NSArray class]]) { 
    users = usersFromJSON; 
} else { 
    NSLog(@"Something wrong with my JSON: %@", usersFromJSON); 
} 
+0

好的。但是這仍然不能解決問題。 – GangstaGraham 2013-02-18 00:26:51

+0

我需要更多的代碼來告訴你什麼是錯的確切(除了你在字典上調用'objectAtIndex') - 發佈你創建用戶的代碼。 – lupatus 2013-02-18 00:28:58