Pwn
Pwn

Reputation: 3445

Declaring a property with no instance variable

Since Xcode automatically adds a @synthesis now, how can I declare a property without an corresponding ivar?

Upvotes: 0

Views: 119

Answers (2)

Max
Max

Reputation: 16719

@interface Obj : NSObject
@property NSString* str;
@end

@implementation

-(NSString*) str {
    //TODO:
    return @"sdfsdf";
}

-(void) setStr:(NSString*) st {
//TODO:
}

@end

Naming convention is simple:

getter has the same name as the property (in example it is str), setter must have set prefix, i.e. setPropertyName (first letter of property name is uppercase: setStr:)

You can also add attributes like atomic, retain, strong etc. but they will have no effect in case you're implementing properties yourself, but it's a good hint for the users of your class.

Upvotes: 1

mattjgalloway
mattjgalloway

Reputation: 34912

You'd need to implement the getter and setter and then it won't be automatically synthesised and an ivar automatically created.

Upvotes: 1

Related Questions