//
// main.m
// KVC
//
// Created by 千雅爸爸 on 16/10/6.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "User.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
//可以很方便的完成类中属性的复制操作,
//同时即使是隐藏的属性也是可以kvc来完成设置的。
User *user = [[User alloc] init];
[user setValue:@"xiaoming" forKey:@"username"];
[user setValue:@"ibm" forKey:@"company"];
[user setValue:@20 forKey:@"age"];
[user setValue:@"male" forKey:@"sex"];
NSLog(@"%@",user);
}
return 0;
}
//
// User.h
// KVC
//
// Created by 千雅爸爸 on 16/10/6.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface User : NSObject
@property(nonatomic,copy) NSString *username;
@property(nonatomic,copy) NSString *company;
//@property(nonatomic,assign) NSInteger age;
//为了方便可以写setvalue forkey的方法,这里使用strong,并且是NSNumber类型
@property(nonatomic,strong) NSNumber *age;
@end
//
// User.m
// KVC
//
// Created by 千雅爸爸 on 16/10/6.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import "User.h"
//匿名的类别,添加一个隐藏的属性,注意这里要写User
@interface User ()
@property (nonatomic,copy) NSString *sex;
@end
@implementation User
- (NSString *)description
{
return [NSString stringWithFormat:@"name=%@,company=%@,age =%@,sex=%@", _username,_company,_age,_sex];
}
@end