Objective-C语法之KVC的使用 .

本文详细介绍了Objective-C中的键值编码(KVC)机制,包括如何使用KVC存取对象属性、通过键路径访问嵌套属性、自动封装基本数据类型以及操作集合。展示了KVC在管理对象属性时的灵活性和便利性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

除了一般的赋值和取值的方法,我们还可以用Key-Value-Coding(KVC)键值编码来访问你要存取的类的属性。

下图来自苹果官网:

[img]
[img]http://dl.iteye.com/upload/attachment/0078/7092/0cb2df5f-c8ff-35ac-992c-aa87036706e0.jpg[/img]
[/img]


如何使用KVC存取对象属性呢?看个示例

1、使用KVC
定义一个Student类,继承于NSObject。

.h文件

#import <Foundation/Foundation.h>

@interface Student : NSObject
{
NSString *name;
}
@end


.m文件
#import "Student.h"
@implementation Student
@end


.m文件也没有实现。name属性没有加property,原来的访问方法就访问不了name属性了。怎么办呢?用kvc就可以了

#import "Student.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Student *student = [[[Student alloc]init ]autorelease];
[student setValue:@"张三" forKey:@"name"];
NSString *name = [student valueForKey:@"name"];
NSLog(@"学生姓名:%@",name);
}
return 0;
}


打印结果:

2012-07-20 15:04:09.920 objectiveC[1977:403] 学生姓名:张三

张三 这个值存进去了,通过valueForKey取出来了。

如果存的时候key和类属性的名称不一致会怎么样呢?

代码改成

[student setValue:@"张三" forKey:@"name1"];

运行,程序崩溃 ,打印:

2012-07-20 15:09:40.432 objectiveC[2069:403] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Student 0x106f14270> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name1.'

提示没有这个name1 这个key。


2、键路径访问属性
如果访问这个类里中的属性中的属性呢?那就用到了键路径

关键字:键路径取值valueForKeyPath 键路径存值:forKeyPath

新建一个类Course,课程类,课程类有课程名称这个属性

.h文件

#import <Foundation/Foundation.h>

@interface Course : NSObject
{
NSString *CourseName;
}
@end


.m文件
#import "Student.h"
@implementation Student
@end



在Student中添加Course属性 ,student.h文件中代码如下:

#import <Foundation/Foundation.h>
@class Course;
@interface Student : NSObject
{
NSString *name;
Course *course;
}
@end


实现还是什么都没有,这里就不贴代码了

在main方法中,我们实验通过键路径访问Course中CourseName的属性

#import "Student.h"
#import "Course.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Student *student = [[[Student alloc]init ]autorelease];
[student setValue:@"张三" forKey:@"name"];
NSString *name = [student valueForKey:@"name"];
NSLog(@"学生姓名:%@",name);

Course *course = [[[Course alloc]init] autorelease];
[course setValue:@"语文课" forKey:@"CourseName"];
[student setValue:course forKey:@"course"];
NSString *courseName = [student valueForKeyPath:@"course.CourseName"];
NSLog(@"课程名称:%@", courseName);

//也可以这样存值
[student setValue:@"数学课" forKeyPath:@"course.CourseName"];
courseName = [student valueForKeyPath:@"course.CourseName"];
NSLog(@"课程名称:%@", courseName);

}
return 0;
}


24.}
#import "Student.h"
#import "Course.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Student *student = [[[Student alloc]init ]autorelease];
[student setValue:@"张三" forKey:@"name"];
NSString *name = [student valueForKey:@"name"];
NSLog(@"学生姓名:%@",name);

Course *course = [[[Course alloc]init] autorelease];
[course setValue:@"语文课" forKey:@"CourseName"];
[student setValue:course forKey:@"course"];
NSString *courseName = [student valueForKeyPath:@"course.CourseName"];
NSLog(@"课程名称:%@", courseName);

//也可以这样存值
[student setValue:@"数学课" forKeyPath:@"course.CourseName"];
courseName = [student valueForKeyPath:@"course.CourseName"];
NSLog(@"课程名称:%@", courseName);

}
return 0;
}
运行打印结果:

2012-07-20 15:33:51.902 objectiveC[2415:403] 学生姓名:张三

2012-07-20 15:33:51.904 objectiveC[2415:403] 课程名称:语文课

2012-07-20 15:33:51.904 objectiveC[2415:403] 课程名称:数学课


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

我们在Student类中添加分数属性 NSInteger point;

.h文件

#import <Foundation/Foundation.h>
@class Course;
@interface Student : NSObject
{
NSString *name;
Course *course;
NSInteger point;
}
@end


.m文件不改变就不管了
下面是main示例了

#import "Student.h"
#import "Course.h"

int main(int argc, const char * argv[])
{
@autoreleasepool {
Student *student = [[[Student alloc]init ]autorelease];
[student setValue:@"张三" forKey:@"name"];
NSString *name = [student valueForKey:@"name"];
NSLog(@"学生姓名:%@",name);

Course *course = [[[Course alloc]init] autorelease];
[course setValue:@"语文课" forKey:@"CourseName"];
[student setValue:course forKey:@"course"];
NSString *courseName = [student valueForKeyPath:@"course.CourseName"];
NSLog(@"课程名称:%@", courseName);

//也可以这样存值
[student setValue:@"数学课" forKeyPath:@"course.CourseName"];
courseName = [student valueForKeyPath:@"course.CourseName"];
NSLog(@"课程名称:%@", courseName);

[student setValue:@"88" forKeyPath:@"point"];
NSString *point = [student valueForKey:@"point"];
NSLog(@"分数:%@", point);

}
return 0;
}


打印结果:

2012-07-20 15:43:19.593 objectiveC[2533:403] 学生姓名:张三

2012-07-20 15:43:19.596 objectiveC[2533:403] 课程名称:语文课

2012-07-20 15:43:19.596 objectiveC[2533:403] 课程名称:数学课

2012-07-20 15:43:19.598 objectiveC[2533:403] 分数:88

我们用NSString*类型设置的属性值@"88",而我们的属性是NSInteger类型的,存取都没有问题。


4、操作集合

在Student类中加入数组NSArray,用来表示其他的学生。这样我们可以添加多个其他的学生,再用集合操作计算学生的分数,最高分,最低分,平均分等

#import <Foundation/Foundation.h>
@class Course;
@interface Student : NSObject
{
NSString *name;
Course *course;
NSInteger point;
NSArray *otherStudent;
}
@end

.m文件实现不变。

在main函数中添加三个学生,添加到数组中,然后求平均分,最高,最低分,学生数量

#import "Student.h"
#import "Course.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Student *student = [[[Student alloc]init ]autorelease];
[student setValue:@"张三" forKey:@"name"];
NSString *name = [student valueForKey:@"name"];
NSLog(@"学生姓名:%@",name);

[student setValue:@"88" forKey:@"point"];
NSString *point = [student valueForKey:@"point"];
NSLog(@"分数:%@", point);

Student *student1 = [[[Student alloc][[Student alloc]init]autorelease];
Student *student3 = [[[Student alloc]init]autorelease];
[student1 setValue:@"65" forKey:@"point"];
[student2 setValue:@"77" forKey:@"point"];
[student3 setValue:@"99" forKey:@"point"];
NSArray *array = [NSArray arrayWithObjects:student1,student2,student3,nil];
[student setValue:array forKey:@"otherStudent"];
NSLog(@"其他学生的成绩%@", [student valueForKeyPath:@"otherStudent.point"]);
NSLog(@"共%@个学生", [student valueForKeyPath:@"otherStudent.@count"]);
NSLog(@"最高成绩:%@", [student valueForKeyPath:@"otherStudent.@max.point"]);
NSLog(@"最低成绩:%@", [student valueForKeyPath:@"otherStudent.@min.point"]);
NSLog(@"平均成绩:%@", [student valueForKeyPath:@"otherStudent.@avg.point"]);
}
return 0;
}


运行打印结果

2012-07-20 16:09:17.101 objectiveC[2857:403] 学生姓名:张三

2012-07-20 16:09:17.104 objectiveC[2857:403] 分数:88

2012-07-20 16:09:17.105 objectiveC[2857:403] 其他学生的成绩(

65,

77,

99

)

2012-07-20 16:09:17.106 objectiveC[2857:403] 共3个学生

2012-07-20 16:09:17.106 objectiveC[2857:403] 最高成绩:99

2012-07-20 16:09:17.107 objectiveC[2857:403] 最低成绩:65

2012-07-20 16:09:17.108 objectiveC[2857:403] 平均成绩:80.33333333utorelease];
Student *student2 = [[[Student alloc]init]autorelease];
Student *student3 = [[[Student alloc]init]autorelease];
[student1 setValue:@"65" forKey:@"point"];
[student2 setValue:@"77" forKey:@"point"];
[student3 setValue:@"99" forKey:@"point"];
NSArray *array = [NSArray arrayWithObjects:student1,student2,student3,nil];
[student setValue:array forKey:@"otherStudent"];
NSLog(@"其他学生的成绩%@", [student valueForKeyPath:@"otherStudent.point"]);
NSLog(@"共%@个学生", [student valueForKeyPath:@"otherStudent.@count"]);
NSLog(@"最高成绩:%@", [student valueForKeyPath:@"otherStudent.@max.point"]);
NSLog(@"最低成绩:%@", [student valueForKeyPath:@"otherStudent.@min.point"]);
NSLog(@"平均成绩:%@", [student valueForKeyPath:@"otherStudent.@avg.point"]);
}
return 0;
}[/code]

运行打印结果

2012-07-20 16:09:17.101 objectiveC[2857:403] 学生姓名:张三

2012-07-20 16:09:17.104 objectiveC[2857:403] 分数:88

2012-07-20 16:09:17.105 objectiveC[2857:403] 其他学生的成绩(

65,

77,

99

)

2012-07-20 16:09:17.106 objectiveC[2857:403] 共3个学生

2012-07-20 16:09:17.106 objectiveC[2857:403] 最高成绩:99

2012-07-20 16:09:17.107 objectiveC[2857:403] 最低成绩:65

2012-07-20 16:09:17.108 objectiveC[2857:403] 平均成绩:80.333333333333333333333333333333333333

还可以求总和 @sum。
资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 在 Linux 系统中,查找域名或主机名对应的 IP 地址是网络管理中的一项基础且关键任务,对于排查网络故障、调试网络问题以及监控网络服务是否正常运行等场景都非常重要。本文将介绍五种在 Linux 终端查询域名 IP 地址的方法。 首先,dig 命令(全称 Domain Information Groper)是一个功能强大的 DNS 查询工具,能够向 DNS 服务器发送查询请求并获取详细的响应信息。如果需要查询单个域名的 IP 地址,可以使用命令 dig 2daygeek.com +short 。此外,还可以通过编写 bash 脚本,将包含域名的文本文件中的域名逐个读取,然后利用 dig 命令进行查询,从而实现批量查询域名 IP 地址的功能。 其次,host 命令是一个简单易用的 DNS 查询工具,主要用于将域名解析为 IP 地址。要获取某个域名的 IP 地址,直接使用 host 2daygeek.com 即可。如果只想显示 IP 地址部分,可以通过管道结合 grep 和 sed 命令来实现,例如:host 2daygeek.com | grep "has address" | sed s/has address/-/g 。 再者,nslookup 命令也是一种常用的 DNS 查询工具,它支持交互式查询 DNS 信息。通过 nslookup 2daygeek.com 可以查询域名的 IP 地址。若要以非交互式的方式只显示 IP 地址,可以使用命令 nslookup 2daygeek.com | awk /^Address:/ {print $2} 。 另外,fping 命令与传统的 ping 命令不同,它不会直接进行 DNS 查询,而是通过发送 ICMP Echo Request(pi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值