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

相关文章

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

Redis的Zset类型及相关命令详细讲解

《Redis的Zset类型及相关命令详细讲解》:本文主要介绍Redis的Zset类型及相关命令的相关资料,有序集合Zset是一种Redis数据结构,它类似于集合Set,但每个元素都有一个关联的分数... 目录Zset简介ZADDZCARDZCOUNTZRANGEZREVRANGEZRANGEBYSCOREZ

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能

模版方法模式template method

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/template-method 超类中定义了一个算法的框架, 允许子类在不修改结构的情况下重写算法的特定步骤。 上层接口有默认实现的方法和子类需要自己实现的方法

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序

迭代器模式iterator

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/iterator 不暴露集合底层表现形式 (列表、 栈和树等) 的情况下遍历集合中所有的元素