Created
December 12, 2013 22:50
-
-
Save calvinlai/7936979 to your computer and use it in GitHub Desktop.
Read Only Properties 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
#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 |
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
#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