KVC模式讲解和Block语法

2024-06-19 02:08
文章标签 讲解 模式 语法 block kvc

本文主要是介绍KVC模式讲解和Block语法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

KVC键值编码,使用完整实例:

#import <Foundation/Foundation.h>

@interface Course : NSObject

{

    NSString* courseName;

}

- (NSString*)description;

@end

 

#import "Course.h"

@implementation Course

- (NSString*)description

{

    NSString* str = [NSString stringWithFormat:@"%@",courseName];

    return str;

}

@end

 

#import <Foundation/Foundation.h>

#import "Course.h"

@interface Student : NSObject

{

    NSString* name;

    Course* course;

    NSInteger point;

    NSArray* otherStudent;

}

- (NSString*)description;

@end

 

#import "Student.h"

@implementation Student

- (NSString*)description

{

    NSString* str = [NSString stringWithFormat:@"name is %@,course is %@,point is %lu",name,course,point];

    return str;

}

@end

 

#import <Foundation/Foundation.h>

#import "Student.h"

#import "Course.h"

 

int main(int argc, const char * argv[])

{

 

@autoreleasepool {

        //使用KVC存储属性值,

        //方式一

        Student* student1 = [[[Student alloc] init] autorelease];

        Course* course1 = [[[Course alloc] init] autorelease];

        [student1 setValue:@"wusong" forKey:@"name"];

        [student1 setValue:@"90" forKey:@"point"];

        [course1 setValue:@"yuwen" forKey:@"courseName"];

        [student1 setValue:course1 forKey:@"course"];

       

        //方式二

        Student* student2 = [[[Student alloc] init] autorelease];

        Course* course2 = [[[Course alloc] init] autorelease];

        [student2 setValue:@"liyang" forKey:@"name"];

        [student2 setValue:course2 forKey:@"course"];

        [student2 setValue:@"shuxue" forKeyPath:@"course.courseName"];

        //自动封装基本数据类型

        [student2 setValue:@"88" forKey:@"point"];

       

        //定义三个学生,存放到数组中,求三个学生成绩的最大值,最小值,平均值

        Student* student = [[[Student alloc] init] autorelease];

        Course* course = [[[Course alloc] init] autorelease];

        [student setValue:@"wusong" forKey:@"name"];

        [student setValue:course forKey:@"course"];

        [student setValue:@"shuxue" forKeyPath:@"course.courseName"];

       

        Student* stu1 = [[[Student alloc] init] autorelease];

        Student* stu2 = [[[Student alloc] init] autorelease];

        Student* stu3 = [[[Student alloc] init] autorelease];

        [stu1 setValue:@"88" forKey:@"point"];

        [stu2 setValue:@"70" forKey:@"point"];

        [stu3 setValue:@"91" forKey:@"point"];

        NSArray* array = [NSArray arrayWithObjects:stu1,stu2,stu3, 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;

}

 

Block语句块

        //当在BLock语句块外的变量用__block声明时,在Block语句块中此变量才可以被修改

        __block int num = 10;

       

        void(^myBlock)(int n1,int n2) = ^(int num1,int num2){

            NSLog(@"%d,%d",num1,num2);

            num++;

            NSLog(@"%d",num);

        };

       

        myBlock(1,2);//调用

 

        //使用Block语法来对数组中的元素进行排序和枚举访问数组中的元素,步骤为:

        NSArray* array = [NSArray arrayWithObjects:@"string1",@"string12",@"string21",@"string01",nil];

        NSComparator comparator = ^(id obj1,id obj2){

            return [obj1 compare:obj2];

        };

       

        NSArray* array1 = [array sortedArrayUsingComparator:comparator];

        NSLog(@"array1:%@",array1);

       

        //枚举,逐一去访问元素

        [array1 enumerateObjectsUsingBlock:^(id obj,NSUInteger index,BOOL* stop){

            NSLog(@"%@",obj);

            NSLog(@"%lu",index);

        }];

 

这篇关于KVC模式讲解和Block语法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1073710

相关文章

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Python基础文件操作方法超详细讲解(详解版)

《Python基础文件操作方法超详细讲解(详解版)》文件就是操作系统为用户或应用程序提供的一个读写硬盘的虚拟单位,文件的核心操作就是读和写,:本文主要介绍Python基础文件操作方法超详细讲解的相... 目录一、文件操作1. 文件打开与关闭1.1 打开文件1.2 关闭文件2. 访问模式及说明二、文件读写1.

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处

C++ 中的 if-constexpr语法和作用

《C++中的if-constexpr语法和作用》if-constexpr语法是C++17引入的新语法特性,也被称为常量if表达式或静态if(staticif),:本文主要介绍C++中的if-c... 目录1 if-constexpr 语法1.1 基本语法1.2 扩展说明1.2.1 条件表达式1.2.2 fa

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

C++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

大数据spark3.5安装部署之local模式详解

《大数据spark3.5安装部署之local模式详解》本文介绍了如何在本地模式下安装和配置Spark,并展示了如何使用SparkShell进行基本的数据处理操作,同时,还介绍了如何通过Spark-su... 目录下载上传解压配置jdk解压配置环境变量启动查看交互操作命令行提交应用spark,一个数据处理框架

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元