Skip to content

Instantly share code, notes, and snippets.

@NSExceptional
Last active July 21, 2016 16:52
Show Gist options
  • Save NSExceptional/d51fe742b5cb3395f9cf to your computer and use it in GitHub Desktop.
Save NSExceptional/d51fe742b5cb3395f9cf to your computer and use it in GitHub Desktop.
An example of an immutable property with mutable storage.
@interface MYClass : NSObject
@property (nonatomic, readonly) NSArray *apples;
@property (nonatomic, readonly) NSArray *oranges;
@end
@interface MYClass () {
// Option 1: private ivar
NSMutableArray *_apples;
}
// Option 2: private property
@property (nonatomic) NSMutableArray *mutableOranges;
@end
@implementation MYClass
- (NSArray *)apples {
return _apples.copy;
}
- (NSArray *)oranges {
return self.mutableOranges.copy;
// or, return _mutableOranges.copy
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment