In what situations will the object not match the encoding of the key value?

I am currently studying everything related to keyword encoding.

The docs say:

Any object in the key path sequence that does not match the encoding of the key value for the corresponding key valueForUndefinedKey: message.

I am trying to imagine a situation where the object does not match the encoding of the keyword. How could this happen? When I subclass UIView, which is obviously compatible, isn't it? But when I just create my own object with NSObject as a superclass, how is it? And when I make a class without a superclass, then surely this does not correspond to kv?

+3
source share
2 answers

If you read it carefully, you will see that it says “key compatibility” for the corresponding key . This means that basically you do not have the appropriate KVC methods for the key you requested. So if I do [[NSString stringWithString:@"foo"] valueForKey:@"dippingSauce"], it will valueForUndefinedKey:because NSString does not match the KVC for the "dippingSauce" key - it does not have an instance method dippingSauceor dippingSauceivar.

+8
source

It says: "This does not match the keyword encoding for the corresponding key ." It means that

@interface MyObject : NSObject {
  NSString *foo;
  NSString *bar;
}

@property (nonatomic, retain) NSString *foo;
@property (nonatomic, retain) NSString *bar;

@end

@interface MyObject

@synthesize foo;
@synthesize bar;

@end

Meets the requirements of "foo" and "bar", but not "baz."

, , NSObject KVC. . , KVC ( , :

NSArray *people = ... ;
NSArray *firstNames = [people valueForKey:@"firstName"];

. , Cocoa ( iPhone), CoreData ( ), - . , KVC , . KVC.

+4

Source: https://habr.com/ru/post/1713213/


All Articles