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

相关文章

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点

Python 函数详解:从基础语法到高级使用技巧

《Python函数详解:从基础语法到高级使用技巧》本文基于实例代码,全面讲解Python函数的定义、参数传递、变量作用域及类型标注等知识点,帮助初学者快速掌握函数的使用技巧,感兴趣的朋友跟随小编一起... 目录一、函数的基本概念与作用二、函数的定义与调用1. 无参函数2. 带参函数3. 带返回值的函数4.

MySQL连表查询之笛卡尔积查询的详细过程讲解

《MySQL连表查询之笛卡尔积查询的详细过程讲解》在使用MySQL或任何关系型数据库进行多表查询时,如果连接条件设置不当,就可能发生所谓的笛卡尔积现象,:本文主要介绍MySQL连表查询之笛卡尔积查... 目录一、笛卡尔积的数学本质二、mysql中的实现机制1. 显式语法2. 隐式语法3. 执行原理(以Nes

RabbitMQ消费端单线程与多线程案例讲解

《RabbitMQ消费端单线程与多线程案例讲解》文章解析RabbitMQ消费端单线程与多线程处理机制,说明concurrency控制消费者数量,max-concurrency控制最大线程数,prefe... 目录 一、基础概念详细解释:举个例子:✅ 单消费者 + 单线程消费❌ 单消费者 + 多线程消费❌ 多

从入门到进阶讲解Python自动化Playwright实战指南

《从入门到进阶讲解Python自动化Playwright实战指南》Playwright是针对Python语言的纯自动化工具,它可以通过单个API自动执行Chromium,Firefox和WebKit... 目录Playwright 简介核心优势安装步骤观点与案例结合Playwright 核心功能从零开始学习

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、