请考虑以下代码:
#import <Foundation/Foundation.h> #import <objc/runtime.h> typedef NSString* MyStringRef; typedef NSString MyString; @interface ClassA : NSObject @property (nonatomic,copy) MyStringRef stringA; @property (nonatomic,copy) MyString *stringB; @end @implementation ClassA @synthesize stringA = _stringA; @synthesize stringB = _stringB; @end int main() { unsigned int count = 0; Ivar *ivars = class_copyIvarList([ClassA class],&count); for (unsigned int i = 0; i < count; i++) { Ivar thisIvar = ivars[i]; NSLog(@"thisIvar = %s,%s",ivar_getName(thisIvar),ivar_getTypeEncoding(thisIvar)); } ClassA *a = [[ClassA alloc] init]; NSLog(@"Out: %@",[a valueForKey:@"stringA"]); NSLog(@"Out: %@",[a valueForKey:@"stringB"]); }
这是输出:
$clang --version Apple clang version 3.1 (tags/Apple/clang-318.0.58) (based on LLVM 3.1svn) Target: x86_64-apple-darwin11.4.0 Thread model: posix $clang -o typedef -fobjc-arc -framework Foundation typedef.m && ./typedef 2012-06-06 20:14:15.881 typedef[37282:707] thisIvar = _stringA,@"NSString" 2012-06-06 20:14:15.884 typedef[37282:707] thisIvar = _stringB,^{NSString=#} 2012-06-06 20:14:15.885 typedef[37282:707] Out: (null) 2012-06-06 20:14:15.888 typedef[37282:707] *** Terminating app due to uncaught exception 'NSUnknownKeyException',reason: '[<ClassA 0x7fabe0501480> valueForUndefinedKey:]: this class is not key value coding-compliant for the key stringB.' *** First throw call stack: ( 0 CoreFoundation 0x00007fff835fef56 __exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff878e5d5e objc_exception_throw + 43 2 CoreFoundation 0x00007fff836891b9 -[NSException raise] + 9 3 Foundation 0x00007fff83e77703 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 240 4 Foundation 0x00007fff83dae38e _NSGetUsingKeyValueGetter + 108 5 Foundation 0x00007fff83dae315 -[NSObject(NSKeyValueCoding) valueForKey:] + 392 6 typedef 0x000000010e84bc6d main + 317 7 typedef 0x000000010e84b9c4 start + 52 )
我在这里遇到的问题是Objective-C是什么导致typedef NSString MyString有效地创建一个包含一个Class类型变量的结构,然后在我使用MyString的地方使用它.例如结构看起来像这样(在咨询this之后):
struct NSString { Class a; };
它有意义,但导致valueForKey:失败,大概是因为它现在是一个结构,所以它不能以与对象相同的方式返回它.或者更确切地说,它属于搜索顺序described in the docs的“抛出异常”部分.
我只想了解导致这种情况发生的语言是什么,以及为什么它不能以同样的方式处理我的2 typedef.
@R_502_323@
我在WWDC 2012上得到了一个答案.事实证明,这种行为与GCC的二进制兼容性是预期的.
对我来说似乎很奇怪,他们会有效地介绍我认为与旧GCC二进制兼容的bug.我希望这种事情最终会逐步取消,以支持正确的编译器.