1

だから、私はこれを持っています:

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

@implementation Foo
@synthesize bars = _bars;
// ... more stuff here ...
@end

そのため、 のインスタンスでFooいっぱいになり、 のインスタンスはコンテナ オブジェクト内に存在します。そして、私は言います:barsBarFoo

[container valueForKeyPath:@"foo.bars.@count"]

私は私に素敵な箱入りの番号を与えることを期待しています. ただし、代わりに次のようになります。

Exception: [<Bar 0xc175b70> valueForUndefinedKey:]: this class is not key value coding-compliant for the key bars.

なぜ?of s をカウント可能Barにするために特別なものを on に実装する必要はないと思いますが、それがこのエラー メッセージが意味することです。ドキュメントには簡単な例しかありませんが、これは同じように機能すると思います。NSArrayBar

4

1 に答える 1

1

私のコードはあなたのコードとどう違うのですか?これが機能するので...

//  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

次に、私のコンテナクラスで:

// .h
@property (strong, nonatomic) Foo *foo;

// .m
_foo = [[Foo alloc] init];
NSNumber *c = [self valueForKeyPath:@"foo.bars.@count"];
NSLog(@"count is %@", c);

ログ=>「カウントは2です」

于 2013-02-13T17:35:45.853 に答える