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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:57:29+00:00 2026-05-15T20:57:29+00:00

I was recently told casting [NSNull null] to (NSString*) was terrible. However, -[NSDictionary stringForKey:]

  • 0

I was recently told casting [NSNull null] to (NSString*) was “terrible”.

However, -[NSDictionary stringForKey:] method will return [NSNull null] if the key is not in the dictionary, and if I don’t do the cast the compiler yells at me.

Am I missing something?

EDIT:

My mistake… and I think I might be beginning to see the problem…

As everyone has pointed out, there is no stringForKey: method on NSDictionary, and yes, I was thinking about the user defaults when I asked the question… so I went back and looked at what I was doing… here it is:

NSString * someValue = (NSString*)[myDictionary objectForKey:@"key"];
if (someValue == (NSString*)[NSNull null]) ...

if I don’t do the cast in this case, I get the following error:

warning: comparison of distinct Objective-C types 'struct NSNull *' and 'struct NSString *' lacks a cast

Casting the value “solves” the problem, and I’d rather not write 10 lines where one will do…

Is this bad? Where will I run into problems?

ALSO:

The dictionaries are coming from a JSON library… and NULLs are valid values in this case, so perhaps it’s not that NSDictionary returns them if the key is missing, but rather that the key is, in fact, there, and the value is actually null.

  • 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-15T20:57:30+00:00Added an answer on May 15, 2026 at 8:57 pm

    NSDictionary does not return instances of NSNull unless you or some other source put them there. As well, NSDictionary does not respond to -stringForKey:. Are you perhaps thinking of NSUserDefaults?

    If you have an instance of NSDictionary and all you want to do is get a string from it, you could try something like the following code.

    NSString *MyGetString(NSDictionary *dict, id key, NSString *fallback) {
        id result = [dict objectForKey: key];
    
        if (!result)
            result = fallback;
        else if (![result isKindOfClass: [NSString class]])
            result = [result description];
    
        return result;
    }
    

    Pass a fallback string as fallback–the function will use that if no object is associated with the key key.

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

Sidebar

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.