我正在尝试按照使用SBJsonParser的教程来获取Twitter提要,并在表格视图中显示它们。
但是我收到以下错误
[__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];`
这就是我用它来把它放到表格视图中
- (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;
}
编辑:
我使用objectatindex的NSLogged statusArray: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;
};
}
答案 0 :(得分:3)
让我们假设您的json解析器没有错误。
-(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 isEqualToString=@""] || [userInfo length]<0])
cell.textLabel.text = @"There is no user info";
else
cell.textLabel.text = userInfo;
return cell;
}
答案 1 :(得分:1)
在您收到的JSON中,键“user”的对象是一个字典,因此当您尝试将该字典分配给标签的text属性(即期望字符串)时会出现错误。你需要做这样的事情:
cell.textLabel.text = [[[statusArray objectAtIndex:indexPath.row]
objectForKey:@"user"]
objectForKey:@"screen_name"];
答案 2 :(得分:0)
“用户”路径是一个字典,需要一个字符串。
cell.textLabel.text = [[statusArray objectAtIndex:indexPath.row] objectForKeyPath:@"user.name"];