[__NSCFDictionary isEqualToString:]: нераспознанный селектор, отправленный экземпляру 0x6b7e510 ' - PullRequest
1 голос
/ 26 марта 2012

Я пытаюсь следовать руководству, которое использует SBJsonParser для получения каналов Twitter и отображает их в виде таблицы.

Но я получаю следующую ошибку

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

это то, что в моем

@ interface NSString *json_string; NSArray *statusArray;

Это код, который я использую, чтобы получить твиттер

`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;
}

РЕДАКТИРОВАТЬ:

Я 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;
};

}

Ответы [ 3 ]

3 голосов
/ 27 марта 2012

Предположим, ваш парсер 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 голос
/ 26 марта 2012

В JSON, который вы получаете, объект для ключа «пользователь» является словарем, поэтому вы получаете ошибку, когда пытаетесь присвоить этот словарь текстовому свойству метки, ожидающему строку. Вам нужно будет сделать что-то вроде этого:

cell.textLabel.text = [[[statusArray objectAtIndex:indexPath.row] 
                          objectForKey:@"user"]
                            objectForKey:@"screen_name"];
0 голосов
/ 26 марта 2012

путь пользователя - это словарь, вам нужна строка.

cell.textLabel.text = [[statusArray objectAtIndex:indexPath.row] objectForKeyPath:@"user.name"];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...