-
-
Save PsychoH13/518fddd28144e58f3974 to your computer and use it in GitHub Desktop.
@Property attributes
This file contains 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> | |
#import <objc/runtime.h> | |
@interface MyClass : NSObject | |
@property(assign) NSString *strAssign; | |
@property(retain) NSString *strRetain; | |
@property(copy) NSString *strCopy; | |
@property(weak) NSString *strWeak; | |
@property(strong) NSString *strStrong; | |
@property(unsafe_unretained) NSString *strUnsafe; | |
@end | |
@implementation MyClass | |
@synthesize strAssign, strRetain, strCopy, strWeak, strStrong, strUnsafe; | |
@end | |
int main (int argc, const char * argv[]) | |
{ | |
@autoreleasepool | |
{ | |
const char *props[] = | |
{ | |
"strAssign", | |
"strUnsafe", | |
"strWeak", | |
"strRetain", | |
"strStrong", | |
"strCopy", | |
NULL | |
}; | |
const char **ptr = props; | |
while(*ptr != NULL) | |
{ | |
objc_property_t prop = class_getProperty([MyClass class], *ptr); | |
NSLog(@"%-9s: %s", *ptr, property_getAttributes(prop)); | |
ptr++; | |
} | |
} | |
return 0; | |
} | |
// The weak property attribute is, according to Apple documentation, reported by the letter W | |
/* Expected output: | |
strAssign: T@"NSString",VstrAssign | |
strUnsafe: T@"NSString",VstrUnsafe | |
strWeak : T@"NSString",W,VstrWeak | |
strRetain: T@"NSString",&,VstrRetain | |
strStrong: T@"NSString",&,VstrStrong | |
strCopy : T@"NSString",C,VstrCopy | |
*/ | |
/* Actual output: | |
strAssign: T@"NSString",VstrAssign | |
strUnsafe: T@"NSString",VstrUnsafe | |
strWeak : T@"NSString",VstrWeak | |
strRetain: T@"NSString",&,VstrRetain | |
strStrong: T@"NSString",&,VstrStrong | |
strCopy : T@"NSString",C,VstrCopy | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment