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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:35:59+00:00 2026-05-22T01:35:59+00:00

how do I set the value of an NSNumber variable (without creating a new

  • 0

how do I set the value of an NSNumber variable (without creating a new object) in objective-c?

Background

  • I’m using Core Data and have a managed object that has an NSNumber (dynamic property)
  • passing (by reference) this to another method which will update it
  • not sure how to update it? if I allocate it another new NSNumber things don’t work, which I guess makes sense it’s then got a pointer to a different object not the core data object (I guess)
  • 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-22T01:36:01+00:00Added an answer on May 22, 2026 at 1:36 am

    An NSNumber object isn’t mutable. This means that the only way to change a property containing a NSNumber is to give it a new NSNumber. To do what you want, you have three options:

    1. Pass the Core Data object to the method and have it directly set the property.

    - (void)updateNumberOf:(MyCoreDataObject *)theObject {
        NSNumber *newNumber = ...; // create new number
        theObject.number = newNumber;
    }
    

    Called as [self updateNumberOf:theCoreDataObject];

    2. Have the update method return a new NSNumber and update it in the caller.

    - (NSNumber *)updateNumber:(NSNumber *)oldNumber {
        NSNumber *newNumber = ...; // create new number
        return newNumber;
    }
    

    Called using:

    NSNumber *theNumber = theCoreDataObject.number;
    theNumber = [self updateNumber:theNumber];
    theCoreDataObject.number = theNumber;
    

    3. Pass a pointer to a number variable and update it in the caller (I would only suggest this over option 2 if you need to return something else).

    - (void)updateNumber:(NSNumber **)numberPointer {
        if(!numberPointer) return; // or possibly throw an error
        NSNumber *oldNumber = *numberPointer;
        NSNumber *newNumber = ...; // create new number
        *numberPointer = newNumber;
    }
    

    Called using:

    NSNumber *theNumber = theCoreDataObject.number;
    [self updateNumber:&theNumber];
    theCoreDataObject.number = theNumber;
    

    I did not bother with memory management in any of these examples. Make sure you release/autorelease objects appropriately.

    4. (from Greg’s comment) Similar to option 1, but passes the key to the update method to be more portable.

    - (void)updateNumberOf:(id)theObject forKey:(NSString *)key {
        NSNumber *oldNumber = [theObject valueForKey:key];
        NSNumber *newNumber = ...; // create new number
        [theObject setValue:newNumber forKey:key];
    }
    

    Called as [self updateNumberOf:theCoreDataObject forKey:@"number"];

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

Sidebar

Related Questions

I am attempting to set a value in a textarea field using jquery with
I have a form action that needs to have its value set from a
How can i set value(in code behind) for an element declared in the xslt
If I put a DateTime value into an Excel cell using Range.set_value through .Net
I'm having trouble trying to set the value of a property after i cast
I wonder if there is a way to set the value of #define in
This page from Adobe says to add a wmode parameter and set its value
As the question says, how do I set the value of a DropDownList control
How can I get Environnment variables and if something is missing, set the value?
How do you set a default value for a MySQL Datetime column? In SQL

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.