编程

当前位置:澳门新莆京娱乐官网 > 编程 > KVC键值编码

KVC键值编码

来源:http://www.drgigabytes.com 作者:澳门新莆京娱乐官网 时间:2020-04-21 12:56

图片 1Objective-C

  • Key Value Coding-NSKeyValueCoding三个非正式的协商,提供一种体制来直接待上访谈对象的习性。
  • KVO 就是基于 KVC 实现的关键能力之一。
  • 它是一种选拔字符串标志符,直接访谈对象属性的建制,并非一向调用getter 和 setter方法。
  • KVC键值编码。多如牛毛我们应用valueForKey 来代表getter 方法,setValue:forKey来替代setter方法潜心: 使用KVC赋值和取值,key和天性名要保持一致,否侧会形成崩溃

一、使用KVC

利用KVC对性能赋值,取值

People.h文件中

#import <Foundation/Foundation.h>@interface People : NSObject{ NSString *name; //属性}@end

KVC使用

#pragma mark //////////KVC使用////////// People *people = [[People alloc] init]; //使用KVC赋值 [people setValue:@"LuisX" forKey:@"name"]; //使用KVC取值 NSString *name = [people valueForKey:@"name"]; NSLog(@"%@", name); //输出: LuisX

二、键路线访问

使用KVC对质量的习性赋值,取值KVC在根据键值路线取值时,会自动层层递进,获取相应的键值

People.h文件中

#import <Foundation/Foundation.h>#import "Address.h"@interface People : NSObject{ NSString *name; Address *address; //添加其他类属性}@end

Address.h文件中

#import <Foundation/Foundation.h>@interface Address : NSObject{ NSString *code;}@end

键路线访谈

#pragma mark //////////键路径访问////////// People *people = [[People alloc] init]; [people setValue:@"LuisX" forKey:@"name"]; NSString *name = [people valueForKey:@"name"]; NSLog(@"%@", name); //输出: LuisX //方式一: 使用setValue...forKey赋值 valueForKeyPath取值 Address *address = [[Address alloc] init]; [address setValue:@"10010" forKey:@"code"]; [people setValue:address forKey:@"address"]; NSString *code1 = [people valueForKeyPath:@"address.code"]; NSLog(@"方式1:%@", code1); //输出: 方式1:10010 //方式二: 使用setValue...forKeyPath赋值 valueForKeyPath取值 [people setValue:@"10010" forKeyPath:@"address.code"]; NSString *code2 = [people valueForKeyPath:@"address.code"]; NSLog(@"方式2:%@", code2); //输出: 方式2:10010

三、自动封装基本数据类型

采纳KVC间接改正对象属性时,系统会活动判定指标属性的等级次序,并产生更改。

People.h文件中

#import <Foundation/Foundation.h>#import "Address.h"@interface People : NSObject{ NSString *name; Address *address; NSInteger number; //添加基本数据类型属性}@end

电动封装基本数据类型

#pragma mark //////////自动封装基本数据类型////////// People *people = [[People alloc] init]; [people setValue:@"88" forKey:@"number"]; NSString *number = [people valueForKey:@"number"]; //NSInteger类型转字符串 NSLog(@"%@", number); //输出: 88

四、操作群集

KVC依照键值路线取值时,借使指标不分包钦定的键值,会活动步入指标内部,查找对象属性

People.h文件中

#import <Foundation/Foundation.h>#import "Address.h"@interface People : NSObject{ NSString *name; Address *address; NSInteger number; NSArray *morePeople; //添加集合类型属性}@end

操作集结

#pragma mark //////////操作集合////////// People *people = [[People alloc] init]; People *p1 = [[People alloc] init]; People *p2 = [[People alloc] init]; People *p3 = [[People alloc] init]; [p1 setValue:@"65" forKey:@"number"]; [p2 setValue:@"77" forKey:@"number"]; [p3 setValue:@"99" forKey:@"number"]; NSArray *array = [NSArray arrayWithObjects:p1, p2, p3, nil]; [people setValue:array forKey:@"morePeople"]; NSLog(@"数组中元素的number值:%@", [people valueForKeyPath:@"morePeople.number"]); NSLog(@"元素个数:%@", [people valueForKeyPath:@"morePeople.@count"]); NSLog(@"元素最大:%@", [people valueForKeyPath:@"morePeople.@max.number"]); NSLog(@"元素最小:%@", [people valueForKeyPath:@"morePeople.@min.number"]); NSLog(@"元素平均:%@", [people valueForKeyPath:@"morePeople.@avg.number"]); //输出: 数组中元素的number值:(65, 77, 99) //输出: 元素个数:3 //输出: 元素最大:99 //输出: 元素最小:65 //输出: 元素平均:80.333333333333333333333333333333333333

进行阅读:Objective-C KVC机制KVC/KVO原理详明及编制程序指南iOS KVC & KVO

本文由澳门新莆京娱乐官网发布于编程,转载请注明出处:KVC键值编码

关键词:

上一篇:没有了

下一篇:浅尝UIAppearance的使用