<span style="font-size:24px;">#import <Foundation/Foundation.h>
#import "Student.h"
@interface Person : NSObject
{
NSString *_name;//姓名
NSString *_gender;//性别
NSInteger _age;//年龄
Student *_stu;//学生
}
@end</span><pre name="code" class="objc"><span style="font-size:24px;">#import "Person.h"
@implementation Person
- (NSString *)description
{
return [NSString stringWithFormat:@"name:%@,gender:%@,age:%ld,stu:%@", _name, _gender, _age, _stu];
}
<span style="background-color: rgb(51, 102, 255);">//当给定的key未匹配到对应的方法以及实例变量时,会自动调用该方法,所以我们只需要实现该方法即可(实现体中可以没有实现内容)</span>
<span style="color:#3366ff;">-(void)setValue:(id)value forUndefinedKey:(NSString *)key</span>
{
}
<span style="background-color: rgb(102, 51, 255);">//当给定的key匹配不到对应的getter方法以及对应的实例变量时,会自动调用该方法</span>
<span style="color:#3366ff;">- (id)valueForUndefinedKey:(NSString *)key</span>
{
//NSLog(@"UndefinedKey");
return nil;
}
@end</span><pre name="code" class="objc"><span style="font-size:24px;">@interface Student : NSObject
/*
{
NSString *_name;//姓名
NSString *_gender;//性别
NSInteger _age;//年龄
}
*/
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *gender;
@property (nonatomic, retain) NSString *mother;
@property (nonatomic, retain) NSString *father;
@property (nonatomic, retain) NSString *school;
@property (nonatomic, retain) NSString *number;
@property (nonatomic) NSInteger age;
@property (nonatomic , retain) NSString *girlFriendName;
@property (nonatomic) CGFloat height;
@property (nonatomic) CGFloat weight;
<span style="background-color: rgb(102, 51, 255);">//字典是服务器给的,通过字典来初始化</span>
-(id)initWithDic:(NSDictionary *)dic;
@end</span><pre name="code" class="objc"><span style="font-size:24px;">#import "Student.h"
@implementation Student
- (NSString *)description
{
return [NSString stringWithFormat:@"name:%@,gender:%@,age:%ld", _name, _gender, _age];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
<span style="background-color: rgb(102, 51, 255);">//字典是服务器给的,通过字典来初始化</span>
<span style="color:#3366ff;">-(id)initWithDic:(NSDictionary *)dic
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dic];
}
return self;
}</span>
<span style="background-color: rgb(102, 51, 255);">//重写父类的dealloc方法</span>
- (void)dealloc
{
[_name release];
[_gender release];
[_father release];
[_mother release];
[_girlFriendName release];
[_number release];
[_school release];
[super dealloc];
}
@end</span><pre name="code" class="objc"><span style="font-size:24px;">#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Person *per = [[Person alloc] init];
Student *stu = [[Student alloc] init];
//KVC:key-value-coding 键值编码,是一种间接访问实例变量的方式,通过指定的key来找到对应的实例变量。(切记:key是字符串)
<span style="background-color: rgb(51, 102, 255);">//KVC的工作原理:</span>根据指定饿key,比如:name
//(1)先查找该类是否又对应的setter方法:比如:setName:如果有,则通过setter方法给实例变量赋值,如果没有则转到(2)
//(2)查找该类有没有一个_+key的实例变量,比如_name,如果有,则给_name赋值,如果没有则转到(3)
//(3)查找该类是否具有一个和key同名的实例变量,比如name,如果有,则给name赋值,如果没有则转到(4)
//(4)如果都没有找到,系统会抛出一个NSUnknownException,说明没有找到可匹配的key,(也就是该类既没有setName,也没有name,也没有_name)。
[per setValue:@"Duck" forKey:@"_name"];
[per setValue:@"男" forKey:@"gender"];
[per setValue:@20 forKey:@"age"];
[per setValue:stu forKey:@"stu"];
NSLog(@"%@", per);
//通过stu给stu的_name赋值
[stu setValue:@"lanou" forKey:@"name"];
NSLog(@"%@", stu);
//通过per给stu的_name赋值
[per setValue:@"Frank" forKeyPath:@"stu.name"];
//相当于:
[[per valueForKey:@"stu"] setValue:@"kg" forKey:@"name"];//getter方法,然后再上面几步</span><pre name="code" class="objc"><span style="font-size:24px;"><span style="background-color: rgb(102, 51, 255);">//笑笑语法</span>
NSDictionary *dic = @{@"name":@"Frank",@"gender":@"man",@"mother":@"lanou",@"father":@"lanou",@"number":@"9527",@"height":@180,@"weight":@72,@"age":@30,@"school":@"lanou",@"girlFriendName":@"aaaa",@"bb": @"gg"};<span style="background-color: rgb(51, 102, 255);">//这个字典是网络服务器给的,不用自己写</span>。
//<span style="background-color: rgb(102, 51, 255);">如果服务器给的键值比自己写的setter方法多的话,重写- (void)setValue:(id)value forUndefinedKey:(NSString *)key方法,内部不写即可</span>
//Student *stu = [[Student alloc] init];
// [stu setValuesForKeysWithDictionary:dic];
Student *stu = [[Student alloc] initWithDic:dic];//.m文件内部初始化了
[stu release];
NSLog(@"%@", stu);
//内部的实现步骤
// dic[key](等价于[dic objectForKey:key])根据key取到key对应的值
// for (NSString *key in dic) {
// [stu setValue:dic[key] forKey:key];
// }
</span>