Last active
December 25, 2015 04:09
-
-
Save between40and2/6914781 to your computer and use it in GitHub Desktop.
Given an Objective-C object and an attribute name, how can you access (read/write) it?
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
return [self.object valueForKey: attrName]; | |
-- | |
SEL getterSEL = NSSelectorFromString( attrName); | |
// http://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown | |
_Pragma("clang diagnostic push") | |
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") | |
NSLog(@"_________%d",index); | |
// SHEMBAOSEnum_ValueType valueType = [[_list_valueType objectAtIndex:index] integerValue]; | |
id value; | |
// if ([self.object respondsToSelector: getterSEL]) { | |
// | |
// NSLog(@"!!! getterSelector cannot be responded: %s", getterSEL); | |
// | |
// return nil; | |
// } | |
Method met = class_getInstanceMethod([self.object class], getterSEL); | |
char * returntype = method_copyReturnType(met); | |
NSLog(@"return type: %s", returntype); | |
JXOCModel_Type_base * returnType = | |
[JXOCModelFactory_Type modelFromString: [NSString stringWithCString: returntype encoding: NSASCIIStringEncoding]]; | |
free(returntype); | |
switch (returnType.kind) { | |
case JXOCEnum_Type_Kind_Primitive: | |
value = [NSNumber numberWithInteger: [self.object performSelector: getterSEL]]; | |
break; | |
case JXOCEnum_Type_Kind_Object: | |
value= [self.object performSelector: getterSEL]; | |
break; | |
default: | |
break; | |
} | |
_Pragma("clang diagnostic pop") | |
return value; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment