Last active
August 29, 2015 14:21
-
-
Save nsforge/71623cb0138a8210cc1f to your computer and use it in GitHub Desktop.
Private designated initializers in Objective-C
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
/* | |
My goal here is to have an immutable model class with an initialiser that is hidden unless you import a private header ("Person+Private.h"). This is useful in the context of providing a library or framework where consuming code only ever has read-only access to instances of this class, and they are never supposed to create instances themselves. | |
*/ | |
… | |
//*********************************** | |
// Person.h | |
// | |
@interface Person : NSObject | |
- (instancetype)init __attribute__((unavailable)); | |
@property (readonly) NSString *name; | |
@property (readonly) NSInteger age; | |
@end | |
… | |
//*********************************** | |
// Person+Private.h | |
// | |
#import "Person.h" | |
@interface Person () | |
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age; | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment