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 6352765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:17:07+00:00 2026-05-24T22:17:07+00:00

I’ve a method with this part of code: NSDictionary *tmpDict = [self getJsonDictionary]; NSInteger

  • 0

I’ve a method with this part of code:

NSDictionary *tmpDict = [self getJsonDictionary];

NSInteger tmpID;

for (NSDictionary *d in tmpDict) {
    tmpID = [[d objectForKey:@"id"] integerValue];
}

tmpDict is a NSDictionary with variable size. If tmpDict contains at least 2 objects (2 NSDictionary) it works well but if it contains only 1 object (1 NSDictionary) throws this exception:

 -[NSCFString objectForKey:]: unrecognized selector sent to instance 0x4e866d0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x4e866d0'

Why this? And how to fix if tmpDict has only 1 object?

Probably for (NSDictionary *d in tmpDict) is the problem and when tmpDict has 1 object d is a NSCFString, but why?

— UPDATE

The problem is when tmpDict has 1 item (one NSDictionary) the for (NSDictionary *d in tmpDict) take every key/value item in the dictionary and this is the reason for exception. Otherwise if tmpDict has 2+ items is a NSDictionary of NSDictionary and it works well. I need a way to fix this still using the for loop. Ideas?

— UPDATE 2*

Actually this code works but it’s redundant:

if ([tmpObject isKindOfClass:[NSDictionary class]]) {
    tmpID = [[tmpObject objectForKey:@"id"] integerValue];
    tmpProp2 = [tmpObject objectForKey:@"prop2"];
    tmpProp3 = [tmpObject objectForKey:@"prop3"];
    tmpProp4 = [tmpObject objectForKey:@"prop4"];
} else if ([tmpObject isKindOfClass:[NSArray class]]) {
    for (NSDictionary *d in tmpObject) {
        tmpID = [[d objectForKey:@"id"] integerValue];
        tmpProp2 = [d objectForKey:@"prop2"];
        tmpProp3 = [d objectForKey:@"prop3"];
        tmpProp4 = [d objectForKey:@"prop4"];
    }
}

It needs an improvement.

  • 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-24T22:17:08+00:00Added an answer on May 24, 2026 at 10:17 pm

    I’m surprised it works at all actually. For dictionaries, fast enumeration enumerates the keys. That’s why it’s telling you that -objectForKey: has been sent to an NSCFString because the key of a dictionary coming from JSON is always a string.

    You should, perhaps, do this:

    for (id d in [tmpDict objectEnumerator])
    {
        // Objects in a JSON dictionary aren't necessarily dictionaries
        if([d isKindOfClass:[NSDictionary class]])
        {
             tmpID = [[d objectForKey:@"id"] integerValue];
        }
    }
    

    One possible reason why it might work with two dictionaries is if the top level item is actually an array, not a dictionary. If you have:

    [ { "id" : 1 }, { "id" : 2 } ]
    

    your JSON parser will give you an NSArray, not an NSDictionary, which can be parsed with your code even though you assume your top level item is a dictionary.

    Edit: Having seen your update, I’m sure this is what is happening. If you have one object, your JSON string is looking like:

    { "id" : 1, "prop2" : "value1", "prop3" : "value1", "prop4" : "value1" }
    

    If you have two objects, it probably looks like:

    [
            { "id" : 1, "prop2" : "value1", "prop3" : "value1", "prop4" : "value1" },
            { "id" : 2, "prop2" : "value1", "prop3" : "value1", "prop4" : "value1" }
    ]
    

    Notice the square brackets, that means JSON array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.