这是我想做的比较操作:
// foobar is the name of an ivar on some class
// i.e. NSDate *foobar;
const char *ivarType = [self getTypeForInstanceVariableWithName:@"foobar"];
const char *objType = @encode(NSDate);
if (strcmp(ivarType, objType) == 0) {
//set value
}
NSLog(@"comparing %s with %s", ivarType, objType);
辅助方法:
- (const char*)getTypeForInstanceVariableWithName:(NSString *)name {
return ivar_getTypeEncoding(class_getInstanceVariable([self class], [name cStringUsingEncoding:NSUTF8StringEncoding]));
}
NSLog 结果:
comparing @"NSDate" with {NSDate=#}
为什么@encode 返回与 ivar_getTypeEncoding() 不同的类型语法?有没有更好的方法来完成这种类型的确定?我一定在这里遗漏了一些东西......谢谢!