For high.KVC, how does not automatically calls the countOf< Key>??

Ask the master
, I see Apple's official KVC documentation. I try to create a NSArray* arr property, the countOfArr and objectInArrAtIndex: methods, does not implement the Arr: method. According to the document said, if I use [[my object valueForKey:@ "arr" count]], he should be automatically calls the countOfArr method ah, but I run he could not call, just like an ordinary NSArray returns count. I don't know if you have not tried? The official document.:

@interface MyObject
{ NSArray* _arr;}
@property(nonatomic,retain) NSArray* arr;
- (void)setArr:(NSArray*) arr;

- (void)setArr:(NSArray*) arr
- (NSUInteger) countOfArr
{ NSLog(@"abc");
return 1;
{return nil;

MyObject* mo=[[MyObject alloc]init];
NSArray* arr=[NSArray arrayWithObjects:@"abc",nil];
[mo setValue:arr forKey:@"arr"];
NSLog(@"",[[mo valueForKey:@"arr"]count]); //There is no automatic call countOfArr

Started by Dwight at December 04, 2016 - 6:44 PM

Additional, the official document is

That period he was
Otherwise (no simple accessor method is found), searches the class of the receiver for methods whose names match the patterns countOf<Key> and objectIn<Key>AtIndex: (corresponding to the primitive methods defined by the NSArray class) and <key>AtIndexes: (corresponding to the NSArray method objectsAtIndexes:).

If the countOf<Key> method and at least one of the other two possible methods are found, a collection proxy object that responds to all NSArray methods is returned. Each NSArray message sent to the collection proxy object will result in some combination of countOf<Key>, objectIn<Key>AtIndex:, and <key>AtIndexes: messages being sent to the original receiver of valueForKey:. If the class of the receiver also implements an optional method whose name matches the pattern get<Key>:range: that method will be used when appropriate for best performance.

Posted by Dwight at December 16, 2016 - 7:43 PM