Last active
January 13, 2018 00:12
-
-
Save jslegendre/3a160c4dc2066ece284ec60da629098c to your computer and use it in GitHub Desktop.
Natively, the objective-c runtime does not have a function to get an instance variable by name so I wrote a function to do so. This works great for inspecting 'concrete' classes.
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
Ivar class_getIvarFromString(Class cl, NSString *__ivar) { | |
unsigned int count; | |
Ivar *ivars = class_copyIvarList(cl, &count); | |
for (int i = 0; i < count; i++) { | |
Ivar ivar = ivars[i]; | |
NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)]; | |
if([ivarName isEqualToString:__ivar]) { | |
free(ivars); | |
return ivar; | |
} | |
} | |
free(ivars); | |
return NULL; | |
} | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
NSUserNotification *n = [[NSUserNotification alloc] init]; | |
Ivar retIvar = class_getIvarFromString(object_getClass(n), @"_imageURL"); | |
printf("%s\n", ivar_getTypeEncoding(retIvar)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment