Skip to content

Instantly share code, notes, and snippets.

@nsforge
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsforge/71623cb0138a8210cc1f to your computer and use it in GitHub Desktop.
Save nsforge/71623cb0138a8210cc1f to your computer and use it in GitHub Desktop.
Private designated initializers in Objective-C
/*
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