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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 515k
  • Answers 515k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If I focus only on error and page not found… May 16, 2026 at 6:27 pm
  • Editorial Team
    Editorial Team added an answer You can not directly call the functions in another application.… May 16, 2026 at 6:27 pm
  • Editorial Team
    Editorial Team added an answer The required file is not in the maven repositories, but… May 16, 2026 at 6:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Someone recently told me, In the past, Google never indexed PHP pages. I don’t
I recently told a friend that I was starting to learn Catalyst (Perl) and
I was recently told that I should use transactions in my application as they
I was recently told it was bad practice to haved marked a number of
I don't develop in XCode very often, and recently MacPorts told me I needed
I've been doing my coding with Xcode 3.2.3 I've recently upgraded my iPad OS
I am a computer science student therefore I do not know that much. I
Recently, I read this article: http://download.oracle.com/javase/tutorial/extra/generics/wildcards.html My question is, instead of creating a method
Recently I had to type up some documentation on .net data providers and ado.net.
Recently i've switched to PHP 5.3+ and after that migration i learned that the

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.