Created
July 30, 2013 07:21
-
-
Save iiiyu/6110935 to your computer and use it in GitHub Desktop.
运行时候获得属性的名称
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
- (NSString *) nameWithInstance:(id)instance | |
{ | |
unsigned int numIvars = 0; | |
NSString *key=nil; | |
Ivar * ivars = class_copyIvarList([self class], &numIvars); | |
for(int i = 0; i < numIvars; i++) { | |
Ivar thisIvar = ivars[i]; | |
const char *type = ivar_getTypeEncoding(thisIvar); | |
NSString *stringType = [NSString stringWithCString:type encoding:NSUTF8StringEncoding]; | |
if (![stringType hasPrefix:@"@"]) { | |
continue; | |
} | |
if ((object_getIvar(self, thisIvar) == instance)) { | |
key = [NSString stringWithUTF8String:ivar_getName(thisIvar)]; | |
break; | |
} | |
} | |
free(ivars); | |
return key; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment