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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:54:53+00:00 2026-05-28T18:54:53+00:00

I am reading documentation that says setValue:forKey: will treat a NULL value as if

  • 0

I am reading documentation that says setValue:forKey: will treat a NULL value as if it is being removed. Yet, I have an example such as the following that is consistently crashing:

[myObj setValue:[aDictionary valueForKey:@"keyDoesNotExist"] forKey:@"anotherKey"];

This line of code crashes if aDictionary does not contain the key keyDoesNotExist. Is there any way around that? What’s the appropriate thing to do here?

myObj is an instance of NSManagedObject. “anotherKey” is a one to many relationship where myObj can contain many of “anotherKey”.

  • 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-28T18:54:54+00:00Added an answer on May 28, 2026 at 6:54 pm

    When you call setValue:forKey: with a nil value, the method -setNilValueForKey: is called. By default (in NSObject’s implementation), this raises an invalid argument exception. You can do something else with a nil value by overriding -setNilValueForKey: and doing whatever you’d like.

    Alternatively, if you call myObj’s anotherKey setter directly, you shouldn’t get an exception (dependent on the implementation of setAnotherKey: of course):

    myObj.anotherKey = [aDictionary valueForKey:@"keyDoesNotExist"]; 
    

    The documentation you’re reading is probably for NSMutableDictionary. NSMutableDictionary’s implementation of setValue:forKey: calls removeObjectForKey: if the value is nil. That’s not true for NSObject subclasses, which I’m presuming is what myObj is.

    This behavior is discussed in the NSKeyValueCoding Protocol Reference documentation.

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

Sidebar

Related Questions

The documentation for the ISAPI SetHeader function says that using '\0' for the value
I am new on JBossCache. Reading the user documentation it says that a listener
While reading up the documentation for dict.copy() , it says that it makes a
While reading documentation on some library, I saw that some library have some feature,
I know from reading Microsoft documentation that the "primary" use of the IDisposable interface
Reading the documentation it seems this might not be possible, but it seems that
Reading Kohana's documentation, I found out that the main difference in 3.0 version is
I have about 200 categories that are nested. I am currently reading the documention
Reading msdn documentation for dictionaries it says : Public static (Shared in Visual Basic)
I have been reading all the Apple documentation on how to handle various events

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.