![]()
//
// main.m
// 13-KVC的使用
//
/**
KVC: Key Value Coding(键值编码):1.取值赋值。2.字典转模型。
*/
#import <Foundation/Foundation.h>
#import "XMGPerson.h"
#import "XMGDog.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
/**
7.取出数组中所有模型的某个属性值
*/
XMGPerson *person1 = [[XMGPerson alloc] init];
person1.name = @"zhangsan"; //常规赋值用点语法
person1.money = 12.99;
XMGPerson *person2 = [[XMGPerson alloc] init];
person2.name = @"zhangsi";
person2.money = 22.99;
XMGPerson *person3 = [[XMGPerson alloc] init];
person3.name = @"wangwu";
person3.money = 122.99;
person3.dog.name = @"浩";
NSArray *allPersons = @[person1, person2, person3];
NSArray *allPersonName = [allPersons valueForKeyPath:@"name"];
NSLog(@"%@", allPersonName);//(zhangsan,zhangsi,wangwu)
/* KVC赋值
[person1 setValue:@"王五" forKey:@"name"];
[person1 setValue:@"19" forKeyPath:@"money"]; // 自动类型转换
NSLog(@"%@-----%.2f", person1.name, person1.money);*/
}
return 0;
}
/**
* 1.把模型转成字典
*/
void test6(){
XMGPerson *person = [[XMGPerson alloc] init];
person.name = @"lurry";
person.money = 21.21;
NSDictionary *dict = [person dictionaryWithValuesForKeys:@[@"name", @"money"]];
NSLog(@"%@", dict);// {money = "21.21";name = lurry;}
}
/**
* 2.取值
*/
void test5(){
XMGPerson *person = [[XMGPerson alloc] init];
person.name = @"张三";
person.money = 12332;
// 利用kvc取值,alueForKeyPath和valueForKey一样,
NSLog(@"%@ --- %.2f", [person valueForKeyPath:@"name"], [[person valueForKey:@"money"] floatValue]);
}
/**
3.作用: 字典转模型
开发中是不建议使用setValuesForKeysWithDictionary:
1> 字典中的key必须在模型的属性中找到
2> 如果模型中带有模型,setValuesForKeysWithDictionary不好使
应用场景: 简单的字典转模型 ---> 框架 (MJExtention)
*/
void test4(){
NSDictionary *dict = @{
@"name" :@"lurry",
@"money" : @189.88,
@"dog" : @{
@"name" : @"wangcai",
@"price" : @8
},
@"books": @[
@{@"name" :@"iOS快速开发", @"price" : @1999},
@{@"name" :@"iOS快速发", @"price" : @199},
@{@"name" :@"iOS快开发", @"price" : @99}
]
};
XMGPerson *person = [[XMGPerson alloc] initWithDict:dict];
NSLog(@"%@", person.dog.class);
[person setValue: @{
@"name" : @"wangcai",
@"price" : @8
} forKeyPath:@"dog"];
}
/**
* 4.利用KVC修改类的私有成员变量(UIPageControl)
*/
void test3(){
XMGPerson *person = [[XMGPerson alloc] init];
// person->_age
// 利用KVC修改类的私有成员变量
[person printAge];
[person setValue:@"88" forKeyPath:@"_age"]; //
[person setValue:@"88" forKeyPath:@"age"]; //age也可以
[person printAge];
}
/**
* 5.利用KVC进行综合赋值
*/
void test2(){
XMGPerson *person = [[XMGPerson alloc] init];
person.dog = [[XMGDog alloc] init];
// person.dog.name = @"旺财";
// KVC赋值
/*
forKey和forKeyPath
1>forKeyPath 包含了所有 forKey 的功能
2>forKeyPath 进行内部的点语法,层层访问内部的属性
3>注意: key值一定要在属性中找到
*/
[person.dog setValue:@"阿黄" forKey:@"name"];
[person setValue:@"旺财" forKeyPath:@"dog.name"];
NSLog(@"%@", person.dog.name);
}
/**
* 6.利用KVC进行简单赋值
*/
void test(){
XMGPerson *person = [[XMGPerson alloc] init];
// 常规赋值
/*
person.name = @"张三";
person.age = 18;
*/
// KVC赋值
[person setValue:@"王五" forKey:@"name"];
[person setValue:@"19" forKeyPath:@"money"]; // 自动类型转换
NSLog(@"%@-----%.2f", person.name, person.money);
}
//
// XMGPerson.h
// 13-KVC的使用
//
#import <Foundation/Foundation.h>
@class XMGDog;
@interface XMGPerson : NSObject
/** 姓名 */
@property (nonatomic, copy) NSString *name;//string用copy
/** 钱 */
@property (nonatomic, assign) float money;
/** 狗 */
@property (nonatomic, strong) XMGDog *dog;//自定义类型用strong类型
/** 序号 */
@property (nonatomic, copy) NSString *no;
- (void)printAge;
- (instancetype)initWithDict: (NSDictionary *)dict;
+ (instancetype)personWithDict: (NSDictionary *)dict;
@end
//
// XMGPerson.m
// 13-KVC的使用
//
#import "XMGPerson.h"
@implementation XMGPerson
{
int _age;//私有成员变量
}
- (instancetype)init{
if (self = [super init]) {
_age = 1;
}
return self;
}
- (void)printAge{
NSLog(@"age:%d", _age);
}
- (NSString *)description{
return [NSString stringWithFormat:@"name:%@----money:%.2f", _name, _money];
}
- (instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
/* 有100个要写100个
self.name = dict[@"name"];
self.money = [dict[@"money"] floatValue];
*/
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)personWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
@end
//
// XMGDog.h
// 13-KVC的使用
//
#import <Foundation/Foundation.h>
@interface XMGDog : NSObject
/** 姓名 */
@property (nonatomic, copy) NSString *name;
/** 价格 */
@property (nonatomic, assign) float price;
@end
//
// XMGDog.m
// 13-KVC的使用
//
#import "XMGDog.h"
@implementation XMGDog
@end