//TZPerson是普通的继承自NSObject的类
TZPerson.h
#import <Foundation/Foundation.h>
@interface TZPerson : NSObject
@property (nonatomic, strong) NSString* name;
@property (nonatomic, assign) int age;
@property (nonatomic, strong) NSString* nick;
@property (nonatomic, assign) float height;
@end
TZPerson.m
@implementation TZPerson
// 赋值key值不存在
- (void) setValue:(id)value forUndefinedKey:(NSString *)key {
NSLog(@"key = %@值不存在 ", key);
}
@end
//在外部view中使用keyValue 来调用TZPerson的属性
ViewController.m
/// KVC字典操作
- (void) dictionaryTest {
TZPerson* p = [TZPerson new];
NSDictionary* dict = @{
@"name":@"Tom",
@"age":@18,
@"nick":@"Cat",
@"height":@180,
@"dd":@"helo"
};
//根据字典里的key给P对象赋对应的value值
[p setValuesForKeysWithDictionary:dict];
NSLog(@"p.name = %@, p.age = %d, p.n
iOS keyValue方法的使用
最新推荐文章于 2023-09-24 18:05:04 发布