Search code examples
iosobjective-cpropertiesencapsulation

readonly public, readwrite private property


I saw this thread, but am not completely clear in iOS7 given the Programming guide says you can leave out the @synthesise keyword for properties.

I want to have a @property that is readonly externally, but readwrite internally. Can I use just the @property keyword like this?

EDIT: What would be considered more correct, or at least more idiomatic, of the answers provided. To have a separate readwrite property in a Class extension or access the ivar directly in the implementation?

EDIT: To show that I am using a Class Extension, as suggested in answers.

//.h
@interface CACustomerAuthenticator : NSObject
@property (nonatomic, copy, readonly) NSString *username;
@end

//.m
@interface CACustomerAuthenticator ()
@property (nonatomic, copy, readwrite) NSString *username;
@end

Solution

  • readonly is enough.

    Using

    @synthesize username = _someVar;
    

    You are allowed to write in username accessing _someVar. If there's no synthesize, then

    @synthesize username = _username;
    

    is automatically generated.