有什么方法可以在Xcode 4.4中获得整洁的Objective-C文字索引功能吗? [英] Is there any way to get the neat Objective-C literal indexing feature in Xcode 4.4?

查看:93
本文介绍了有什么方法可以在Xcode 4.4中获得整洁的Objective-C文字索引功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了有关新的Objective-C文字的全部信息,并使用Xcode转换了我的旧代码,但是索引代码没有改变.我手动更改了它,但是它无法编译.我看到一个帖子,说我们必须等到iOS 6,但现在我要索引了!

I read all about the new Objective-C literals, and used Xcode to convert my old code, but the indexing code didn't change. I changed it by hand but then it wouldn't compile. I saw a post that said we have to wait until iOS 6, but I want the indexing NOW!

有什么解决办法吗?

推荐答案

嗯,有一种方法可以做到!将索引方法添加为NSArray和NSDictionary的类别,即可获得所需的大多数类的功能.您可以在此处上阅读ObjectiveC文字.还要感谢James Webster为@YES和@NO提供的解决方案,您现在也可以在您的项目中正确使用它们! (该技术)

Well, there is a way to do it! Add the indexing methods as a category to NSArray and NSDictionary, and you can get the feature for most of the classes you'd want it for. You can read up on ObjectiveC literals here. And thanks to James Webster's solution for @YES and @NO you can use them properly in your projects now too! (the technique)

1)创建接口文件

// NSArray+Indexing.h
#if !defined(__IPHONE_6_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
@interface NSArray (Indexing)
- (id)objectAtIndexedSubscript:(NSUInteger)idx;
@end
@interface NSMutableArray (Indexing)
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;
@end
// NSDictionary+Indexing.h
@interface  NSDictionary (Indexing)
- (id)objectForKeyedSubscript:(id)key;
@end
@interface  NSMutableDictionary (Indexing)
- (void)setObject:(id)obj forKeyedSubscript:(id)key;
@end
#endif

2)创建实现文件////在执行此操作之前,请参见下面的编辑-您可以跳过

2) Create the Implementation files // see edit below before doing this - you can skip this

// NSArray+Indexing.m
#if !defined(__IPHONE_6_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
#import "NSArray+Indexing.h"
@implementation NSArray (Indexing)
- (id)objectAtIndexedSubscript:(NSUInteger)idx
{
    return [self objectAtIndex:idx];
}
@end
@implementation NSMutableArray (Indexing)
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx
{
    [self replaceObjectAtIndex:idx withObject:obj];
}
@end

// NSMutableDictionary+Indexing.m
@implementation  NSDictionary (Indexing)

- (id)objectForKeyedSubscript:(id)key
{
    return [self objectForKey:key];
}
@end
@implementation  NSMutableDictionary (Indexing)
- (void)setObject:(id)obj forKeyedSubscript:(id)key
{
    [self setObject:obj forKey:key];
}
@end
#endif

3)将接口文件添加到您的pch文件中以供全局使用,或根据需要将它们添加到.m文件中

3) Add the Interface files to your pch file for global use, or add them as needed to .m files

// Add to PCH file
#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
...
#if !defined(__IPHONE_6_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0

// New Indexing
#import "NSDictionary+Indexing.h"
#import "NSArray+Indexing.h"

// Provided by James Webster on StackOverFlow
#if __has_feature(objc_bool) 
#undef YES 
#undef NO 
#define YES __objc_yes 
#define NO __objc_no 
#endif 

#endif
#endif

#endif

4)重建,然后添加以下文件以验证其全部正常

4) Rebuild, then add the below files to verify that it all works

// Test Example
{

    NSMutableArray *a = [NSMutableArray arrayWithArray:@[ @"a", @"b", @"c" ]];
    NSLog(@"%@", a[1]);
    a[1] = @"foo";
    NSLog(@"a: %@", a);

    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{ @"key" : @"object" }];
    NSLog(@"%@", dict[@"key"]);

    dict[@"key"] = @"New Object";
    dict[@"newKey"] = @"WOW a new object";

    NSLog(@"dict: %@", dict);

    NSLog(@" %@ %@", @YES, @NO );
}

好吧,根据苹果公司一位重要的llvm/clang工程师说,已经有一个库已经与实现链接在一起,所以您只需要接口文件即可:

Well, according to a key llvm/clang Apple engineer, there is a library that already gets linked in with the implementations, so you just need the interface file:

日期:2012年8月20日,星期一15:16:43-0700 来自:格雷格·帕克(Greg Parker) 到: ... 主题:Re:如何在iOS 5上使Obj-C集合下标工作? ...

Date: Mon, 20 Aug 2012 15:16:43 -0700 From: Greg Parker To: ... Subject: Re: How to make Obj-C collection subscripting work on iOS 5? ...

作为一个实验,我为这些方法添加了类别@interface,但未添加@implementation —应用程序仍然运行良好(至少在5.1仿真器中)

As an experiment I added the category @interface for these methods, but not the @implementation — the app still ran fine (at least in the 5.1 simulator)

编译器发出相同的调用.神奇之处在于名称越来越不准确的libarclite(不仅仅是ARC Anymore™"),它在运行时添加了下标方法的实现(如果尚不存在).

The compiler emits the same calls. The magic is in the increasingly inaccurately named libarclite ("It's Not Just For ARC Anymore™"), which adds implementations of the subscripting methods at runtime if they don't already exist.

IIRC有一些libarclite不会升级的可下标的类(也许是NSOrderedSet?),因此您仍然需要在较早的部署目标上进行全面测试.

IIRC there are some subscript-able classes that libarclite does not upgrade (NSOrderedSet, maybe?) so you still need to test thoroughly on older deployment targets.

这篇关于有什么方法可以在Xcode 4.4中获得整洁的Objective-C文字索引功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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