k v c
pragma mark KVC---key value coding 鍵值編碼
// kvc就是給類中的屬性賦值
Teacher *tec = [[Teacher alloc]initWithName:@"liuxing" age:23 sex:@"M"];
NSLog(@"===%@",tec.name);
// 鍵值對賦值法
[tec setValue:@"w" forKey:@"sex"];
NSLog(@"%@",tec.sex);
// 通過key獲取屬性對應的值
[tec valueForKey:@"sex"];
NSLog(@"%@",tec.sex);
[tec setValue:@"zhansan" forKey:@"name"];
NSLog(@"%@",tec);
// 設置student屬性的值
Student *stu = [[Student alloc]init];
[tec setValue:stu forKey:@"stu"];//屬性名
stu.name = @"haha";
tec.student.name =@"haha";
// 通過路徑賦值
[tec setValue:@"haha" forKeyPath:@"student.name"];//屬性的路徑
// 通過路徑獲取值
NSString *path= [tec valueForKeyPath:@"stu.name"];
//用字典來設置多個屬性的值
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"name",@"liuxing",@"sex",@"M", nil];
[tec setValuesForKeysWithDictionary:dic];//常用的方法,這個方法我們要自己在.m中去實現(xiàn)
[tec setValue:@"123" forKey:@"name2"];
NSLog(@"字典獲取值 = %@",tec);