RickiG
RickiG

Reputation: 11390

@synthesize syntax when overriding accessors

I am doing a value object / Entity, that holds data for my model. I get the data from a web service as JSON, now, instead of moving all the different objects from the parsed JSON over into different properties on my Entity. i.e. reading out the NSString for the @"name" key and setting it to [Entity setName:[JSONDictionary objectForKey:@"name"] etc. My Entity has one actual property, NSDictionary *dataDictionary, this property hold the JSON dictionary as it left the parser.

Now when I need the name value I write an accessor that looks like this:

- (NSString*) name {
    return [self.dataDictionary objectForKey:@"name"];
}

This is nice, I don't have to do any work unless there is a request for that particular property.

Now my question is how do I best tell the compiler that the accessor exists, but does not hold a "real" property. I have this in my interface:

@property(nonatomic, retain) NSString *name;

And the @synthesize in my implementation, but this seems to create an overhead in my logic. Objective C will, as far as I understand, make a room in memory for me to store an object of type NSString when I do the @property(nonatomic, retain) and technically I don't need this as I am already storing this value in the the NSDictionary *dataDictionary

If I make it @dynamic I guess I would also have to provide a setter, which I would never need.

So, Is there a syntax that lets me create the illusion to all objects accessing the Entity that these are "normal" properties, but internally in the Entity not alloc/store unnecessary objects or write more code than is needed?

Upvotes: 0

Views: 339

Answers (1)

Rog
Rog

Reputation: 18670

Declare a name method in your interface, not a property.

- (void)name;

Upvotes: 1

Related Questions