- (nullable id)valueForKey:(NSString *)key; //直接通过Key来取值
在对象的属性中通过key来寻找,来取属性中的值
- (void)setValue:(nullable id)value forKey:(NSString *)key; //通过Key来设值
通过key来将setValue中的值赋值给它,即给对象的属性赋值
- (nullable id)valueForKeyPath:(NSString *)keyPath; //通过KeyPath来取值
在对象的属性中通过keyPath来寻找,来取属性中的值
- (void)setValue:(nullable id)value forKeyPath:(NSString *)keyPath; //通过KeyPath来设值
通过keyPath来将setValue中的值赋值给它,即给对象的属性赋值
valueForKey: 取值是找和指定 key 同名的 property accessor
如果是这样一个 dict:
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"theValue"
forKey:@"@theKey"];// 注意这个 key 是以 @ 开头
NSString *value1 = [dict objectForKey:@"@theKey"];
NSString *value2 = [dict valueForKey:@"@theKey"];
valueForKey: 取值是找和指定 key 同名的 property accessor,会自动忽略掉@,直接找theKey 这个key而不是@theKey
会使程序崩溃。