Last active
July 21, 2016 16:52
-
-
Save NSExceptional/d51fe742b5cb3395f9cf to your computer and use it in GitHub Desktop.
An example of an immutable property with mutable storage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface MYClass : NSObject | |
@property (nonatomic, readonly) NSArray *apples; | |
@property (nonatomic, readonly) NSArray *oranges; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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