如何在iOS上启用新的Objective-C对象文字? [英] How to enable the new Objective-C object literals on iOS?

查看:430
本文介绍了如何在iOS上启用新的Objective-C对象文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Xcode 4.4创建一个新项目并添加以下行时:

When I create a new project with Xcode 4.4 and add these lines:

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比较了两个项目的目标构建设置,但没有任何东西突然出现在我身上。

I compared both projects' target build settings but nothing leapt out at me.

更新:
成功编译的新项目是OSX。我尝试了另一个新的iOS用于上面的行,它无法编译,与我预先存在的(iOS)项目相同。

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.

推荐答案

这与旧项目和新项目无关,而是您使用的SDK的一个因素。您遇到的问题是虽然这是一个编译器功能,但它需要SDK支持。 iOS 5 SDK不提供这种支持,但是iOS 6 SDK也是如此。

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.

因此,现在你应该只使用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.

您需要做的就是添加一个头文件,以便编译器尝试调用。没有必要添加实现;它由 arclite 自动处理。 (如果您不使用ARC,则必须强制链接器包含 arclite 。但您仍然不必实际切换到它。)

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.)

创建一个新的界面文件 NSObject + subscripts.h

Create a new interface file, 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

我把这个块放在 github

注意:我曾建议添加所需的方法到在解释如何仅将它们添加到相关对象之前, NSObject 。回想起来,我认为这是我的错误;它导致错误在运行时捕获而不是编译时间,这与此处介绍的方法不同。这种方法仍然在我的博客上,但我现在相信它是更有用的方法而不是有用的方法。

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.

资料来源:


  • < a href =http://petersteinberger.com/blog/2012/using-subscripting-with-Xcode-4_4-and-iOS-4_3/> Peter Steinberger,使用Xcode 4.4和iOS 4.3+订阅

  • Peter Steinberger, Using Subscripting With Xcode 4.4 and iOS 4.3+

这篇关于如何在iOS上启用新的Objective-C对象文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆