当我使用Xcode 4.4创建一个新项目并添加以下行时:
NSDictionary *test = @{ @"key" : @"test value" };
NSString *value = test[@"key"];
NSLog(@"value is: %@", value);
Run Code Online (Sandbox Code Playgroud)
它编译时没有警告并按预期执行.
将相同的行添加到现有项目会产生编译器错误:
NSString *value = test[@"key"]; <-- Expected method to read dictionary element not found on object of type 'NSDictionary *'
Run Code Online (Sandbox Code Playgroud)
我比较了两个项目的目标构建设置,但没有任何东西在我身上跳跃.
更新: 成功编译的新项目是针对OSX的.我用上面的代码尝试了另一个新的iOS版本,它无法编译,就像我之前存在的(iOS)项目一样.
这个问题的答案似乎不适用于xcode 4.5.总结一下,XCode是否有办法警告只有比部署目标更高版本的类,方法和程序?
做一些调试我打印出一个NSDictionary变量的描述,它给出了:
(NSDictionary *) labelAttrs = 0x00000001
Run Code Online (Sandbox Code Playgroud)
有人可以澄清为什么会这样1吗?
我理解nil或对象指针,但为什么1?
UPDATE
代码:
NSDictionary *labelAttrs = @{NSForegroundColorAttributeName:[UIColor darkGrayColor]};
Run Code Online (Sandbox Code Playgroud)
它在iOS5上运行时崩溃但不是iOS6但是这样:
似乎说你可以使用iOS5的新文字(只要你针对iOS6构建并使用Xcode> = 4.5并使用最新的LLVM进行编译 - 例如看屏幕抓取).而且,根据Apple的说法,我应该可以使用它们:https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/ObjCAvailabilityIndex/index.html
以下是它在Xcode中的外观:

然后我走过去:
注意:这给了我同样的崩溃:
NSDictionary *labelAttrs = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], NSForegroundColorAttributeName, [UIFont fontWithName:CUSTOMFONT size:size], NSFontAttributeName, [NSNumber numberWithInt:0], NSStrokeWidthAttributeName, nil];
Run Code Online (Sandbox Code Playgroud)
删除此(以及以下2行代码)意味着应用程序运行时不会崩溃.但显然没有归因于字符串.
更新:已解决.在iOS5上无法使用归因字符串(至少对于UILabel而言):在iOS 6之前NSAttributedString是否可用?