Skip to content

Instantly share code, notes, and snippets.

@calvinlai
Created December 12, 2013 22:50
Show Gist options
  • Save calvinlai/7936979 to your computer and use it in GitHub Desktop.
Save calvinlai/7936979 to your computer and use it in GitHub Desktop.
Read Only Properties in Objective-C
#import <Foundation/Foundation.h>
@interface TestClass : NSObject
@property (nonatomic, readonly) NSString *propertyA;
@property (nonatomic, readonly) NSString *propertyB;
@property (nonatomic, readonly, getter = propertyC) NSString *propertyC;
@end
#import "TestClass.h"
@interface TestClass ()
@property (nonatomic, readwrite) NSString *propertyB;
@end
@implementation TestClass
- (id)init
{
if (self = [super init])
{
_propertyA = @"value"; // Can't do self.propertyA = @"value"; as propertyA is readonly
_propertyB = @"value";
self.propertyB = @"value"; // self.propertyB = @"value"; will work as we synthesize propertyB in private category as read write
}
return self;
}
- (NSString *)propertyC
{
return @"value";
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment