public
Created

Private property example

  • Download Gist
PrivateProp.h
Objective-C
1 2 3 4 5 6
// PrivateProp.h
 
// Public API for your class
@interface PrivatePropExample : NSObject
// Obviously no property declared here
@end
PrivateProp.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// PrivateProp.m
 
// Class extension. Obviously by definition this is private and only known inside the .m file and can't be used externally
@interface PrivatePropExample ()
@property(nonatomic, retain) NSString* somePrivateProperty;
@end
 
// Class definition
@implementation PrivatePropExample
@synthesize somePrivateProperty = _somePrivateProperty; // Actually not needed anymore since LLVM 4.0
 
// Example internal usage
-(void)setGreetingsForName:(NSString*)name
{
self.somePrivateProperty = [NSString stringWithFormat:@"Hello, %@", name];
}
 
// in not under ARC, release your property backing variable in dealloc
#if ! __has_feature(objc_arc)
-(void)dealloc
{
[_somePrivateProperty release];
[super dealloc];
}
#endif
@end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.