xcode 如何在 iOS 上启用新的 Objective-C 对象字面量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11658669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable the new Objective-C object literals on iOS?
提问by darrinm
When I create a new project with Xcode 4.4 and add these lines:
当我使用 Xcode 4.4 创建一个新项目并添加这些行时:
NSDictionary *test = @{ @"key" : @"test value" };
NSString *value = test[@"key"];
NSLog(@"value is: %@", value);
it compiles with no warnings and executes as expected.
它编译时没有警告并按预期执行。
Adding the same lines to an existing project produces the compiler error:
将相同的行添加到现有项目会产生编译器错误:
NSString *value = test[@"key"]; <-- Expected method to read dictionary element not found on object of type 'NSDictionary *'
I compared both projects' target build settings but nothing leapt out at me.
我比较了两个项目的目标构建设置,但没有任何反应。
Update:The new project that successfully compiled was for OSX. I tried another new one for iOS with the above lines and it fails to compile, same as my pre-existing (iOS) project.
更新:成功编译的新项目适用于 OSX。我使用上述几行尝试了另一个适用于 iOS 的新项目,但它无法编译,与我预先存在的 (iOS) 项目相同。
回答by Steven Fisher
This has nothing to do with old vs. new project, but rather is a factor of the SDK you use. The problem you're running into is that while this is a compiler feature, it requires SDK support. The iOS 5 SDK does not provide that support, though the iOS 6 SDK does.
这与旧项目与新项目无关,而是您使用的 SDK 的一个因素。您遇到的问题是,虽然这是一个编译器功能,但它需要 SDK 支持。尽管 iOS 6 SDK 提供,但 iOS 5 SDK 不提供该支持。
For that reason, now you should just use the iOS 6 SDK. Read on if you want to use object subscripting with the iOS 5 SDK.
因此,现在您应该只使用 iOS 6 SDK。如果您想在 iOS 5 SDK 中使用对象下标,请继续阅读。
All you need to do is add a header file so that the compiler will try the call. There's no need to add an implementation; it's handled automatically by arclite
. (If you are not using ARC, you will have to force the linker to include arclite
. But you still don't have to actually switch to it.)
您需要做的就是添加一个头文件,以便编译器尝试调用。无需添加实现;它由 自动处理arclite
。(如果您不使用 ARC,则必须强制链接器包含arclite
。但您仍然不必实际切换到它。)
Create a new interface file, NSObject+subscripts.h.
创建一个新的接口文件NSObject+subscripts.h。
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 60000
@interface NSDictionary(subscripts)
- (id)objectForKeyedSubscript:(id)key;
@end
@interface NSMutableDictionary(subscripts)
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;
@end
@interface NSArray(subscripts)
- (id)objectAtIndexedSubscript:(NSUInteger)idx;
@end
@interface NSMutableArray(subscripts)
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;
@end
#endif
I've put this chunk on github.
我已经把这个块放在github 上。
Note: I used to suggest adding the required methods to NSObject
before explaining how to add them only to the relevant objects. In retrospect, I believe this was an error on my part; it led to errors being caught at runtime rather than compile time, unlike the approach now presented here. That approach is still on my blog, but I now believe it to be more of a cool hack than a useful approach.
注意:NSObject
在解释如何将它们仅添加到相关对象之前,我曾经建议添加所需的方法。回想起来,我认为这是我的错误;它导致错误在运行时被捕获,而不是在编译时被捕获,这与此处介绍的方法不同。这种方法仍然在我的博客上,但我现在相信它更像是一种很酷的技巧,而不是一种有用的方法。
Source:
来源: