Objective c 使用KVC';数组上的s@count从';数组中有一个数组

Objective c 使用KVC';数组上的s@count从';数组中有一个数组,objective-c,cocoa,nsarray,key-value-coding,Objective C,Cocoa,Nsarray,Key Value Coding,所以,我有这个: @interface Foo @property(nonatomic, readonly, retain) NSArray* bars; @end @implementation Foo @synthesize bars = _bars; // ... more stuff here ... @end 因此Foo用Bar的实例填充Bar,并且Foo的实例存在于容器对象中。然后我说: [container valueForKeyPath:@"foo.bars.@count"]

所以,我有这个:

@interface Foo
@property(nonatomic, readonly, retain) NSArray* bars;
@end

@implementation Foo
@synthesize bars = _bars;
// ... more stuff here ...
@end
因此
Foo
Bar
的实例填充
Bar
,并且
Foo
的实例存在于容器对象中。然后我说:

[container valueForKeyPath:@"foo.bars.@count"]
我希望能给我一个漂亮的盒装号码。然而,我得到的却是:

Exception: [<Bar 0xc175b70> valueForUndefinedKey:]: this class is not key value coding-compliant for the key bars.
异常:[valueForUndefinedKey:]:此类不符合键栏的键值编码。

那么,为什么呢?我不希望必须在
Bar
上实现任何特殊功能才能使
Bar
NSArray
可计数,但这就是错误消息的含义。文档中只有一些微不足道的例子,但我希望它能以同样的方式工作。

我的代码与您的代码有什么不同?因为这很有效

//  Foo.h
#import <Foundation/Foundation.h>

@interface Foo : NSObject
@property(nonatomic, readonly, retain) NSArray* bars;
@end


//  Foo.m
#import "Foo.h"

@interface Foo ()
// extended property decl here, to be readable, but this didn't seem to matter in my test
@property(nonatomic, retain) NSArray* bars;
@end


@implementation Foo

- (id)init {

    self = [super init];
    if (self) {
        _bars = [NSArray arrayWithObjects:@"bar one", @"bar none", nil];
    }
    return self;
}

@end

Logs=>“计数为2”

我的代码与你的代码有何不同?因为这很有效

//  Foo.h
#import <Foundation/Foundation.h>

@interface Foo : NSObject
@property(nonatomic, readonly, retain) NSArray* bars;
@end


//  Foo.m
#import "Foo.h"

@interface Foo ()
// extended property decl here, to be readable, but this didn't seem to matter in my test
@property(nonatomic, retain) NSArray* bars;
@end


@implementation Foo

- (id)init {

    self = [super init];
    if (self) {
        _bars = [NSArray arrayWithObjects:@"bar one", @"bar none", nil];
    }
    return self;
}

@end

Logs=>“count为2”

无论出于何种原因,
@“foo”
返回的对象类型为
Bar
,而不是
foo
类型。你确定没有在层次结构中出错吗?同意,容器对象的实现是什么样子的?容器只有一个@synthesis for Foo:
@属性(非原子、只读、保留)Foo*Foo此外,如果我为这样的条创建一个访问器:
-(NSArray*)条{return\u bars}
并在其上设置一个断点,它会在KVC操作期间被命中,就在抛出异常之前。如果我说
@“foo.someInteger”
,也可以正常工作。无论出于什么原因,
@“foo”
返回的对象类型为
Bar
,而不是
Foo
类型。你确定没有在层次结构中出错吗?同意,容器对象的实现是什么样子的?容器只有一个@synthesis for Foo:
@属性(非原子、只读、保留)Foo*Foo此外,如果我为这样的条创建一个访问器:
-(NSArray*)条{return\u bars}
并在其上设置一个断点,它会在KVC操作期间被命中,就在抛出异常之前。如果我说
@“foo.someInteger”
,也可以正常工作。好的,我只想说这是正确的答案,因为它回答了我的问题,也就是说,“它应该工作。”所以,我需要调试。显然,我的项目中的代码比我包含的代码多得多。我想我已经把它归结到最低限度了,但是很明显一些意想不到的事情正在发生。好吧,我只想说这是正确的答案,因为它回答了我的问题,也就是说,“它应该工作。”所以,我需要调试。显然,我的项目中的代码比我包含的代码多得多。我原以为我已经把它降到了最低限度,但显然发生了意想不到的事情。