2012-03-26 49 views
0

我试图按照使用SBJsonParser获取Twitter提要,并将它们显示在表视图中的教程。[__NSCFDictionary isEqualToString:]:无法识别的选择器发送到实例0x6b7e510'

但我收到以下错误

[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x6b7e510

这是什么在我的

@interface NSString *json_string; NSArray *statusArray;

这是代码即时通讯使用来获取Twitter的饲料

`SBJsonParser *parser = [[SBJsonParser alloc] init]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json"]]; 

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 


json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

statusArray = [parser objectWithString:json_string error:nil];` 

而这正是利用将其放到一个表视图IM

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return statusArray.count; 
} 

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


cell.textLabel.text = [[statusArray objectAtIndex:indexPath.row] objectForKey:@"user"]; 

return cell; 
} 

编辑:

我NSLogged statusArray与objectatindex:0和我有

{ 
contributors = "<null>"; 
coordinates = "<null>"; 
"created_at" = "Mon Mar 26 19:43:54 +0000 2012"; 
favorited = 0; 
geo = "<null>"; 
id = 184365079062528000; 
"id_str" = 184365079062528000; 
"in_reply_to_screen_name" = "<null>"; 
"in_reply_to_status_id" = "<null>"; 
"in_reply_to_status_id_str" = "<null>"; 
"in_reply_to_user_id" = "<null>"; 
"in_reply_to_user_id_str" = "<null>"; 
place = "<null>"; 
"retweet_count" = 0; 
retweeted = 0; 
source = "<a href=\"http://blackberry.com/twitter\" rel=\"nofollow\">Twitter for BlackBerry\U00ae</a>"; 
text = "Kalau tidur jam segini engga mungkin bisa mimpi-___-"; 
truncated = 0; 
user =  { 
    "contributors_enabled" = 0; 
    "created_at" = "Tue Apr 27 18:28:24 +0000 2010"; 
    "default_profile" = 0; 
    "default_profile_image" = 0; 
    description = "27June!"; 
    "favourites_count" = 0; 
    "follow_request_sent" = "<null>"; 
    "followers_count" = 733; 
    following = "<null>"; 
    "friends_count" = 331; 
    "geo_enabled" = 1; 
    id = 137772903; 
    "id_str" = 137772903; 
    "is_translator" = 0; 
    lang = en; 
    "listed_count" = 0; 
    location = ""; 
    name = "Faisal Maulana \U2606"; 
    notifications = "<null>"; 
    "profile_background_color" = 050505; 
    "profile_background_image_url" = "http://a0.twimg.com/profile_background_images/451260814/428031_258048570950875_100002372012794_560610_1320195241_n.jpg"; 
    "profile_background_image_url_https" = "https://si0.twimg.com/profile_background_images/451260814/428031_258048570950875_100002372012794_560610_1320195241_n.jpg"; 
    "profile_background_tile" = 1; 
    "profile_image_url" = "http://a0.twimg.com/profile_images/1962687246/IMG01822-20120319-1126_normal.jpg"; 
    "profile_image_url_https" = "https://si0.twimg.com/profile_images/1962687246/IMG01822-20120319-1126_normal.jpg"; 
    "profile_link_color" = 0a0a0a; 
    "profile_sidebar_border_color" = 161717; 
    "profile_sidebar_fill_color" = ffffff; 
    "profile_text_color" = 050505; 
    "profile_use_background_image" = 1; 
    protected = 0; 
    "screen_name" = IcalHuba; 
    "show_all_inline_media" = 0; 
    "statuses_count" = 60850; 
    "time_zone" = Greenland; 
    url = "<null>"; 
    "utc_offset" = "-10800"; 
    verified = 0; 
}; 

}

+1

因为JSON解析器返回一个自动释放对象,所以你的statusArray在作用域结束时被取消分配。 - 保留它。 – 2012-03-26 19:28:53

+0

但ARC禁止保留? – Huntaz556 2012-03-26 19:33:28

+0

你确定你正在得到statusArray或它的零吗?你能告诉我们statusArray的价值吗? – 2012-03-26 19:45:13

回答

3

让我们假设你的json解析器是无bug的。

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
     return 1 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
     return statusArray.count; 
} 

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

     //note: valueForKeyPath return nil, if key does not find 
     NSString *userInfo = [[statusArray objectAtIndex:indexPath.row] valueForKeyPath:@"user.name"]; // or valueForKeyPath:@"user.screen_name" 

     if ([userInfo [email protected]""] || [userInfo length]<0]) 
      cell.textLabel.text = @"There is no user info"; 
     else 
      cell.textLabel.text = userInfo; 
     return cell; 
} 
0

“用户”路径是一个字典,你需要一个字符串。

cell.textLabel.text = [[statusArray objectAtIndex:indexPath.row] objectForKeyPath:@"user.name"]; 
1

在您收到的JSON,对于关键的“用户”的对象是一本字典,所以当您尝试分配词典标签的文本属性,它期望得到一个字符串得到错误。你需要这样做:

cell.textLabel.text = [[[statusArray objectAtIndex:indexPath.row] 
          objectForKey:@"user"] 
          objectForKey:@"screen_name"]; 
相关问题