美文网首页
KVC使用小例子

KVC使用小例子

作者: 叶子扬 | 来源:发表于2017-01-23 11:37 被阅读0次
实例介绍

创建一个学生对象和一个课程对象,演示:

  • 1、KVC基本使用
  • 2、键路径取值使用场景
  • 3、自动封装基本数据类型
  • 4、操作集合

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

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    Student *student = [[Student alloc] init ];
    
    // 1、使用KVC
    [student setValue:@"猪哥哥" forKey:@"name"];
    NSString *name = [student valueForKey:@"name"];
    NSLog(@"学生姓名:%@",name);
    
    Course *course = [[Course alloc] init];
    [course setValue:@"语文课" forKey:@"CourseName"];
    [student setValue:course forKey:@"course"];
    NSString *courseName = [student valueForKeyPath:@"course.CourseName"];
    NSLog(@"课程名称:%@", courseName);
    
    // 2、键路径访问属性
    [student setValue:@"数学课" forKeyPath:@"course.CourseName"];
    courseName = [student valueForKeyPath:@"course.CourseName"];
    NSLog(@"课程名称:%@", courseName);
    
    // 3、自动封装基本数据类型
    [student setValue:@"88" forKeyPath:@"point"];
    NSString *point = [student valueForKey:@"point"];
    NSLog(@"分数:%@", point);
    
    // 4、操作集合【在Student类中加入数组NSArray,用来表示其他的学生。这样我们可以添加多个其他的学生,再用集合操作计算学生的分数,最高分,最低分,平均分等】
    Student *student1 = [[Student alloc] init ];
    Student *student2 = [[Student alloc] init ];
    Student *student3 = [[Student alloc] init ];
    [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"]);
    NSLog(@"总成绩:%@", [student valueForKeyPath:@"otherStudent.@sum.point"]);
}
@end

其中:Student类和Course类头文件(.m文件什么也不实现,Course也是一样)


#import <UIKit/UIKit.h>

@class Course;
@interface Student : NSObject
{
    NSString *name;
    Course *course;
    NSInteger point;
    NSArray *otherStudent;
}
@end

#import <Foundation/Foundation.h>

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

打印结果:

2017-01-23 11:41:36.899 KVC1[12304:3170161] 学生姓名:猪哥哥
2017-01-23 11:41:36.900 KVC1[12304:3170161] 课程名称:语文课
2017-01-23 11:41:36.900 KVC1[12304:3170161] 课程名称:数学课
2017-01-23 11:41:36.900 KVC1[12304:3170161] 分数:88
2017-01-23 11:41:36.901 KVC1[12304:3170161] 其他学生的成绩(
    65,
    77,
    99
)
2017-01-23 11:41:36.901 KVC1[12304:3170161] 共3个学生
2017-01-23 11:41:36.901 KVC1[12304:3170161] 最高成绩:99
2017-01-23 11:41:36.902 KVC1[12304:3170161] 最低成绩:65
2017-01-23 11:41:36.902 KVC1[12304:3170161] 平均成绩:80.333333333333333333333333333333333333
2017-01-23 11:41:36.903 KVC1[12304:3170161] 总成绩:241

参考:Objective-C语法之KVC使用

相关文章

  • KVC使用小例子

    实例介绍 创建一个学生对象和一个课程对象,演示: 1、KVC基本使用 2、键路径取值使用场景 3、自动封装基本数据...

  • KVC详解

    KVC 目录结构KVC定义KVC取值和设置KVC使用keyPathKVC处理字典KVC作用 参考:iOS KVC和...

  • 说一下KVC和KVO

    本篇采用简单的例子,来介绍 iOS 中的 KVC 和 KVO 的用法和实现原理。 一、KVC 1. KVC是什么 ...

  • iOS原理篇(二): KVC实现原理

    KVC实现原理 什么是 KVC KVC基本使用 KVC 原理 总结 一 、 什么是KVC KVC的全称是Key-V...

  • 数据存储

    1.1.3.使用通知传值 1.2.Segue使用 2. KVC&&KVO2.1.什么是KVC KVC - Key ...

  • OC语法:KVC的底层实现

    一、KVC是什么二、怎么使用KVC三、KVC的底层实现四、KVC常见面试题 一、KVC是什么 KVC全称Key-V...

  • KVC

    方法交换 KVC的使用 KVC的原理 KVC自定义 参考资料 DIS_KVC_KVO[https://github...

  • KVC,KVO

    主要分成一下几个部分1.KVC1.1 KVC简介1.2 KVC使用2.KVO2.1 KVO简介2.2 KVO使用 ...

  • KVO和KVC的使用及原理解析

    一 KVO基本使用 二 KVO本质原理讲解及代码验证 三 KVC基本使用 四 KVC设值原理 五 KVC取值原理 ...

  • KVC的简单使用

    KVC字典转模型 KVC 中经常使用的就是字典转模型 KVC的大招 KVC设置对象属性及取值 KVC间接设置对象属...

网友评论

      本文标题:KVC使用小例子

      本文链接:https://www.haomeiwen.com/subject/shrabttx.html