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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:34:33+00:00 2026-05-25T15:34:33+00:00

I have an NSManagedObject subclass MyClass with a property myProp , which is defined

  • 0

I have an NSManagedObject subclass MyClass with a property myProp, which is defined @dynamic. There are various instances of reading myProp in my code, via [myClass myProp].

Now, I want to define a getter (that returns myProp after appending something to it) for myProp, without changing the various calls to [myClass myProp]. i.e. without creating a getter that is named something other than getMyProp.

My question is, if I create a getter getMyProp, which will override the getter created by NSManagedObject, how do I access the original value that is stored in the database?

  • 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-25T15:34:33+00:00Added an answer on May 25, 2026 at 3:34 pm

    To access the underlying values of a managed object you use the following two methods:

    – (id)primitiveValueForKey:(NSString *)key

    – (void)setPrimitiveValue:(id)value forKey:(NSString *)key

    This is often used to convert NSNumber attributes into their ‘real’ type, for example a bool property:

    - (BOOL)isShared
    {
        [self willAccessValueForKey:@"isShared"];
        NSNumber *underlyingValue = [self primitiveValueForKey:@"isShared"];
        [self didAccessValueForKey:@"isShared"];
        return [underlyingValue boolValue];
    }
    

    The willAccessValueForKey: and didAccessValueForKey: are required by the underlying managed object class for handling faults and relationships etc.

    And if you do end up writing a setter, you must also wrap the accessor in KVC methods:

    - (void)setShared:(BOOL)isShared
    {
        NSNumber *newUnderlyingValue = [NSNumber numberWithBool:isShared];
        [self willChangeValueForKey:@"isShared"];
        [self setPrimitiveValue:newUnderlyingValue forKey:@"isShared"];
        [self didChangeValueForKey:@"isShared"];
    }
    

    Having said this, I would personally not recommend you keep the same method name unless you have a good reason. For ‘derived’ values you generally want to create a brand new method with a different name. It doesn’t take long to do a quick find/replace throughout your code.

    EDIT: added willAccessValueForKey:/didAccessValueForKey: (thanks jrturton)

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

Sidebar

Related Questions

I have one subclass of NSManagedObject like following and stored some instances by NSManagedObjectContext.
I have an NSManagedObject subclass which stores URIs of child objects. It initially stores
I have an NSArrayController bound to a property of an NSManagedObject subclass. The subclass
I have a ConfiguracaoDaApp class in my project that is a NSManagedObject subclass. I
I have a NSManagedObject subclass named Tour. I stored the reference to it using
I have a CoreData NSManagedObject subclass, TextNarration, that has an attribute of type NSString,
In my Objective-C project I have a Country class (NSManagedObject subclass). When comparing a
When I have a subclass of NSManagedObject, I could do this: Friend *newFriend =
I have a subclass of UIView which draws itself based on data held in
Say I have an NSManagedObject subclass called Item . Whenever an item instance is

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.