Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7047109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:43:52+00:00 2026-05-28T02:43:52+00:00

I have the following // this code is inside cellForRowAtIndexPath for a TableViewController id

  • 0

I have the following

// this code is inside cellForRowAtIndexPath for a TableViewController

id answer = [self.answers objectAtIndex:indexPath.row];
if ([answer respondsToSelector:@selector(objectForKey)]) {
    cell.textLabel.text = [answer valueForKey:@"answer_id"];
} else {
    // I'm ending up here, instead of the cell.textLabel being set
    [NSException raise:@"Answer is of invalid class" format:@"It should be able to respond to valueForKey, class: %@", [answer class]];
}

where self.answers is set to

// the question that gets passed here is a parsed single object 
// from the `/questions` path
- (NSArray *)answersForQuestion:(NSDictionary *)question {
    NSString *contents = [self loadContentsForPath:[question valueForKey:@"question_answers_url"]];
valueForKey:@"question_answers_url"]];
    NSDictionary *data = [contents JSONValue];
    NSArray *answers = [data valueForKey:@"answers"];
    return answers;
}

- (NSString *)loadContentsForPath:(NSString *)path {
    NSString *wholeURL = [@"http://api.stackoverflow.com/1.1" stringByAppendingString:path];    
    return [NSString stringWithContentsOfURL:[NSURL URLWithString:wholeURL] encoding:NSUTF8StringEncoding error:nil];
}

I’m doing exactly the same thing for loading questions which works just fine, but it seems
to fail on answers when I try to do [answers valueForKey:@"answer_id"].

I don’t think this is a problem with the JSON parser, because it works fine for the /questions data.

When the debugger stops on the exception and when I try to right click -> Print Description on answers, I get

Printing description of answer:
<CFBasicHash 0x6ec1ec0 [0x1474b38]>{type = mutable dict, count = 13,
entries =>
    1 : <CFString 0x6ec57d0 [0x1474b38]>{contents = "down_vote_count"} = <CFNumber 0x6e1dc00 [0x1474b38]>{value = +0, type = kCFNumberSInt32Type}
    2 : <CFString 0x6ec4ee0 [0x1474b38]>{contents = "last_activity_date"} = <CFNumber 0x6ec5780 [0x1474b38]>{value = +1326379080, type = kCFNumberSInt64Type}
    3 : <CFString 0x6ec44b0 [0x1474b38]>{contents = "community_owned"} = <CFBoolean 0x1474f68 [0x1474b38]>{value = false}
...

which to me seems like a regular hash. I tried both objectForKey and valueForKey and neither of them work, i.e.

exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x6b2a330'

when I do just

cell.textLabel.text = [answer objectForKey:@"answer_id"];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:43:53+00:00Added an answer on May 28, 2026 at 2:43 am

    The : is a part of the method name, so you need to do:

    if ([answer respondsToSelector:@selector(objectForKey:)]) {
    

    And then use objectForKey:, not valueForKey:. The first is to access objects in the dictionary, the later is for the so-called Key-Value Coding. So it’s:

    id answer = [self.answers objectAtIndex:indexPath.row];
    if ([answer respondsToSelector:@selector(objectForKey:)]) {
        cell.textLabel.text = [answer objectForKey:@"answer_id"];
    } else {
        [NSException raise:@"Answer is of invalid class" format:@"It should be able to respond to objectForKey:, class: %@", [answer class]];
    }
    

    Last but not least, it looks like the object you get out of the answer dictionary is a NSNumber, not an NSString. So you might want to change the setting of the text to:

    cell.textLabel.text = [[answer objectForKey:@"answer_id"] description];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code inside my UIViewController - [flipButton addTarget:self.navigationController.delegate action:@selector(changeModeAction:) forControlEvents:UIControlEventTouchUpInside]; As
I have ASP.Net code similar to the following (this is inside a FIELDSET): <ol>
I have the following code inside a class: this.buttons = { cancelButton:{buttonId: cancelButton, buttonText:Cancel,
I have the following code: Bear in mind that while this code works on
I have the following code in Ruby. I want to convert this code into
This is a code review question more then anything. I have the following problem:
I have the following code to do this, but how can I do it
I have the following code: public virtual void Initialise() { this.AddHeader(SystemContext, this.UserSettings.SystemContext); } public
I have the following code: foreach (var control in this.Controls) { } I want
I have the following code that sets a cookie: string locale = ((DropDownList)this.LoginUser.FindControl(locale)).SelectedValue; HttpCookie

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.