KVC设计模式(给对象的属性赋值)
#import <Foundation/Foundation.h>
#import "dog.h"
#import "App.h"
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// KVC 是一种设计模式,key - Value - Coding
//
dog * dogOne = [[dog alloc] init];
dogOne.name = @"two dog";
dogOne.age = 1;
// dog.kind = 1; 会报错
NSLog(@"----%@",dogOne);
//keyValueCoding
dog *dogTwo = [[dog alloc] init];
[dogTwo setValue:@"threeFat" forKey:@"name"];
[dogTwo setValue:@2 forKey:@"age"];
[dogTwo setValue:@"二哈"forKey:@"Kind"];
NSLog(@"%@",dogTwo);
#warning 用KVC的时候需要注意一点,不要写错名字
//给一个模型的多个属性进行赋值
NSDictionary *dic = @{
@"applicationId": @"725879978",
@"slug": @"ridge-racer-slipstream",
@"name": @"山脊赛车:滑流",
@"releaseDate": @"2014-05-21",
@"version": @"2.1.0",
@"description": @"终极街机赛车游戏系列为您的 iOS 设备带来游戏机般的体验!利用低压气穴赶超你的对手并以 150 MPH 速度漂移过急弯!\r\n",
@"categoryId": @"0",
@"categoryName": @"全部",
@"iconUrl": @"http://photo.candou.com/i/114/db489f4affd6dea31910541bb015e2ae",
@"itunesUrl": @"https://itunes.apple.com/cn/app/ridge-racer-slipstream/id725879978?mt=8&uo=4",
@"starCurrent": @"4.5",
@"releaseNotes": @"• 此版本解决了一些游戏崩溃问题。\n• 此应用程序被优化为 iPhone 4S, iPad 3, iPod Touch 5th gen 及以上 要求 IOS 7.0+\n• 修复了一些小错误。",
};
//属性批量赋值
App *app = [[App alloc] init];
//
// for (NSString *key in dic) {
//
// [app setValue:dic[key] forKey:key];
//
//
// }
[app setValuesForKeysWithDictionary:dic];
NSLog(@"=======%@",app);
}
#pragma mark ---pereson---
Person *p = [[Person alloc] init];
[p setValue:@"长亭外,古道边,芳草天" forKey:@"name"];
//这个方法就是可以查找子模型的属性
[p setValue: @"尼古丁"forKeyPath:@"dogThree.name"];
[p setValue:@1 forKeyPath:@"dogThree.age"];
[p setValue:@"哈士奇" forKeyPath:@"dogThree.Kind"];
NSLog(@"%@",p);
return 0;
}
dog类
#import <Foundation/Foundation.h>
@interface dog : NSObject
@property (nonatomic,copy)NSString*name;//名字
@property (nonatomic,assign)NSInteger age;//年龄
@property (nonatomic,copy,readonly)NSString *Kind; //品种
@end
#import "dog.h"
@implementation dog
- (NSString *)description{
return [NSString stringWithFormat:@"---%@,2222%lu,3333%@",self.name,self.age,self.Kind];
}
@end
App类
#import <Foundation/Foundation.h>
@interface App : NSObject
@property (nonatomic,copy)NSString*name;
@property (nonatomic,copy)NSString*iconUrl;
@property (nonatomic,copy)NSString*releaseNotes;
@property (nonatomic,copy)NSString*desc;
@end
#import "App.h"
@implementation App
//属性赋值的时候,如果没有这个key 会触发这个方法
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
NSLog(@"哦哦哦哦哦%@",key);
//如果给未定义的description赋值,其实给desc赋值
if ([key isEqualToString:@"description"]) {
[self setValue:value forKey:@"desc"];
}
}
- (NSString *)description{
//return [NSString stringWithFormat:@"---%@,2222%lu,3333%@",self.name,self.age,self.Kind];
// name;
// iconUrl;
// releaseNotes;
// desc;
return [NSString stringWithFormat:@"--%@,--%@,--%@,--%@,",self.name,self.iconUrl,self.releaseNotes,self.desc];
}
@end
#import <Foundation/Foundation.h>
#import "dog.h"
@interface Person : NSObject
@property (nonatomic,copy)NSString*name;
@property (nonatomic,strong)dog *dogThree;
@end
Person类
#import "Person.h"
@implementation Person
- (instancetype)init{
self= [super init];
if (self) {
self.dogThree = [[dog alloc]init];
}
return self;
}
-(NSString *)description{
return [NSString stringWithFormat:@"%@养了%@",self.name,self.dogThree];
}
@end
KVC设计模式(给对象的属性赋值.png
网友评论