键值编码获取列表中的元素

时间:2013-08-22 18:05:20

标签: ios objective-c key-value-coding

我有两个对象:

@interface AObject : NSObject

@property NSArray *bObjects;

@end

@interface BObject : NSObject

@property NSString *name;

@end

AObject的实例上使用键值编码,我可以获得bObjects@"self.bObjects")列表和bObjects个名称列表({ {1}})。

但是,我想要的只是@"self.bObjects.name"中第一个的名称。我的直觉是键值编码应支持列表下标,如下所示:bObjects

但这似乎并不存在。我如何获得单个实体;使用键值编码的@"bObjects[0].name"的第一个AObject的名称?

脚注:我在上一个问题中意识到我愚蠢地将NSPredicate和KV编码混为一谈。

2 个答案:

答案 0 :(得分:1)

正如Martin R在评论中提到的,目前最好的选择是在firstBObject类中创建AObject属性。

<强> AObject.h /米

@class BObject;

@interface AObject : NSObject
+ (AObject*)aObjectWithBObjects:(NSArray*)bObjects;
@property NSArray *bObjects;
@property (nonatomic, readonly) BObject *firstBObject;
@end

@implementation AObject
+ (AObject*)aObjectWithBObjects:(NSArray*)bObjects
{
    AObject *ao = [[self alloc] init];
    ao.bObjects = bObjects;
    return ao;
}
- (BObject*)firstBObject
{
    return [self.bObjects count] > 0 ? [self.bObjects objectAtIndex:0] : nil;
}
@end

<强> BObject.h /米

@interface BObject : NSObject
+ (BObject*)bObjectWithName:(NSString*)name;
@property NSString *name;
@end

@implementation BObject
+ (BObject*)bObjectWithName:(NSString *)name
{
    BObject *bo = [[self alloc] init];
    bo.name = name;
    return bo;
}
@end

<强>用法:

NSArray *aobjects = @[
                      [AObject aObjectWithBObjects:@[
                       [BObject bObjectWithName:@"A1B1"],
                       [BObject bObjectWithName:@"A1B2"],
                       [BObject bObjectWithName:@"A1B3"],
                       [BObject bObjectWithName:@"A1B4"]
                       ]],
                      [AObject aObjectWithBObjects:@[
                       [BObject bObjectWithName:@"A2B1"],
                       [BObject bObjectWithName:@"A2B2"],
                       [BObject bObjectWithName:@"A2B3"],
                       [BObject bObjectWithName:@"A2B4"]
                       ]],
                      [AObject aObjectWithBObjects:@[
                       [BObject bObjectWithName:@"A3B1"],
                       [BObject bObjectWithName:@"A3B2"],
                       [BObject bObjectWithName:@"A3B3"],
                       [BObject bObjectWithName:@"A3B4"]
                       ]]
                      ];
NSLog(@"%@", [aobjects valueForKeyPath:@"firstBObject.name"]);

<强>结果

  


      A1B1,
      A2B1,
      A3B1

答案 1 :(得分:1)

事实证明,我有幸能够简单地覆盖根类(-valueForKey:)中的AObject。需要重复的是-valueForKeyPath:在每个键上调用-valueForKey:,这很酷。

由于这可能不适用于所有人,并且这可能是对默认预期行为的过多操纵,因此绝对不是“正确”的答案。

但无论如何它在这里:

- (id)valueForKey:(NSString *)string
{
    if ([string characterAtIndex: [string length] - 1] == ']') // Trying to subscript
    {
        NSRegularExpression *subscriptRegex = [[NSRegularExpression alloc] initWithPattern: @"([a-zA-Z]+)\\[([0-9]+)\\]"
                                                                                   options: (NSRegularExpressionOptions)0
                                                                                     error: nil];

        NSString *key = [subscriptRegex stringByReplacingMatchesInString: string
                                                                 options: (NSMatchingOptions)0
                                                                   range: NSMakeRange(0, [string length])
                                                            withTemplate: @"$1"];
        id valueForKey = [self valueForKey: key];
        if (!key || !valueForKey || ![valueForKey respondsToSelector: @selector(objectAtIndexedSubscript:)])
            return nil;

        NSInteger index = [[subscriptRegex stringByReplacingMatchesInString: string
                                                                    options: (NSMatchingOptions)0
                                                                      range: NSMakeRange(0, [string length])
                                                               withTemplate: @"$2"] integerValue];
        if ((index < 0) || (index >= [valueForKey count]))
            return nil;

        return [valueForKey objectAtIndexedSubscript: index];
    }

    return [super valueForKey: string];
}