node ninja
node ninja

Reputation: 32986

making class members properties and synthesizing them

Is it safe to say that if a class member does not need getter or setter functions then there's no point in making them properties and synthesizing them?

Upvotes: 0

Views: 137

Answers (1)

Anshu Chimala
Anshu Chimala

Reputation: 2800

Well, yes, but often properties can be helpful in the implementation itself even if the properties won't be set outside of the implementation.

For example, suppose you had

@interface SomeObject : NSObject {
    NSThing *thing;
}
@end

@implementation SomeObject

- (id)init {
    if((self = [super init]))
        thing = [[NSThing someThing] retain];
    return self;
}

- (void)someMethod {
    if(thing)
        [thing release];
    thing = [[NSThing someOtherThing] retain];
}

// etc etc

@end

Why would you want to bother having to check if thing had been allocated, release thing, set it to something else, and then retain it again, when you could simply do:

- (id)init {
    if((self = [super init]))
        [self setThing:[NSThing someThing]];
    return self;
}

- (void)someMethod {
    [self setThing:[NSThing someOtherThing]];
}

If you don't want to make these properties accessible outside of your class, you can use a category

@interface SomeObject ()
@property (retain) NSThing *thing;
@end

in your .m file.

Upvotes: 2

Related Questions