Runtime 、 Block
1 使用Block方式,对学生对象进行排序。
1.1 问题
在iOS4.0+ 和Mac OS X 10.6+ 中添加了Block概念,以对C语言进行扩展。在Block中可以定义参数列表、返回类型,还可以获取被定义在的作用域内的局部变量的值,并且能修改使用__block修饰的局部变量的值。
Block本质上是一个变量,该变量中存储的数据是一段函数体。
1.2 方案
本案例要求定义一个学生类TRSTudent,然后创建该类的三个对象。用这三个对象建立一个数组,使用Block变量对这个数组排序。并将排序结果输出到控制台上。
1.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:定义类TRStudent
首先,在Day05工程中新添加TRStudent.h文件,用于定义新的类TRStudent。
代码如下所示:
- #import <Foundation/Foundation.h>
- @interface TRStudent : NSObject
- @property(nonatomic,assign)int age;
- @property(nonatomic,copy)NSString* name;
- @end
在上述代码中,以下代码:
- @property(nonatomic,assign)int age;
在TRStudent类中定义了一个是整型属性age,用于存储学生的年龄。它有两个参数,一个是nonatomic,它代表对属性赋值的时候不加锁,即在多线程环境下访问时可能会出现数据错误,如果需要在多线程环境下运行,为保证数据不会出现错误,可使用atomic参数,它会在对属性赋值的时候加锁。另一个参数是assign,对于C语言的基本数据类型,只能选取这个参数。
在上述代码中,以下代码:
- @property(nonatomic,copy)NSString* name;
在TRStudent类中定义了一个是NSString类的对象name,用于存储学生的姓名。它有两个参数,一个是nonatomic,另一个参数是copy,该参数一般用于NSObject类及其子类的对象,这些对象在赋值时实现深拷贝,即属性name指向的对象是赋值给它的对象的副本。
然后,在类TRStudent的实现部分,即在TRStudent.m文件中,重写description方法的实现,该方法用于在NSLog中用%@输出TRStudent类的对象值。
代码如下所示:
- #import "TRStudent.h"
- @implementation TRStudent
- -(NSString *)description{
- return [NSString stringWithFormat:@"name:%@ age:%d",self.name, self.age];
- }
- @end
步骤二:在主程序中创建三个学生对象
代码如下所示:
- #import <Foundation/Foundation.h>
- #import "TRStudent.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- // insert code here...
- TRStudent* stu1 = [[TRStudent alloc]init];
- stu1.age = 18;
- stu1.name = @"zhangsan";
- TRStudent* stu2 = [[TRStudent alloc]init];
- stu2.age = 19;
- stu2.name = @"lisi";
- TRStudent* stu3 = [[TRStudent alloc]init];
- stu3.age = 20;
- stu3.name = @"wangwu";
- }
- return 0;
- }
步骤三:在主程序中创建排序Block
代码如下所示:
- #import <Foundation/Foundation.h>
- #import "TRStudent.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- // insert code here...
- TRStudent* stu1 = [[TRStudent alloc]init];
- stu1.age = 18;
- stu1.name = @"zhangsan";
- TRStudent* stu2 = [[TRStudent alloc]init];
- stu2.age = 19;
- stu2.name = @"lisi";
- TRStudent* stu3 = [[TRStudent alloc]init];
- stu3.age = 20;
- stu3.name = @"wangwu";
- NSComparisonResult(^compare)(id stu1,id stu2);
- compare = ^(id stu1,id stu2)
- {
- //类型转换
- NSString* s1 = nil;
- if([stu1 isMemberOfClass:[TRStudent class]])
- {
- s1 = ((TRStudent*)stu1).name;
- }
- NSString* s2 = nil;
- if([stu2 isMemberOfClass:[TRStudent class]])
- {
- s2 = ((TRStudent*)stu2).name;
- }
- return [s1 compare:s2];
- };
- }
- return 0;
- }
上述代码中,以下代码:
- NSComparisonResult(^compare)(id stu1,id stu2);
声明了Block变量compare,其中NSComparisonResult为Block的返回值类型。^为Block类型说明符,就像定义指针时使用*号一样。(id stu1,id stu2)为Block的形式参数。
Block本质上是变量,但因为其存储的数据是一段代码,所以它有返回值类型和形式参数。
上述代码中,以下代码:
- compare = ^(id stu1,id stu2)
- {
- //类型转换
- NSString* s1 = nil;
- if([stu1 isMemberOfClass:[TRStudent class]])
- {
- s1 = ((TRStudent*)stu1).name;
- }
- NSString* s2 = nil;
- if([stu2 isMemberOfClass:[TRStudent class]])
- {
- s2 = ((TRStudent*)stu2).name;
- }
- return [s1 compare:s2];
- };
是实现了Block。以下语句:
- compare = ^(id stu1,id stu2)
是对Block变量compare的赋值,将一段代码赋值给这个变量。在这段代码中,以下代码:
- NSString* s1 = nil;
- if([stu1 isMemberOfClass:[TRStudent class]])
- {
- s1 = ((TRStudent*)stu1).name;
- }
是对形参stu1的类型检查,如果stu1是TRStudent类的对象,则将stu1中的name属性值赋值给s1。
步骤四:在主程序中对创建的数组排序
代码如下所示:
- #import <Foundation/Foundation.h>
- #import "TRStudent.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- // insert code here...
- TRStudent* stu1 = [[TRStudent alloc]init];
- stu1.age = 18;
- stu1.name = @"zhangsan";
- TRStudent* stu2 = [[TRStudent alloc]init];
- stu2.age = 19;
- stu2.name = @"lisi";
- TRStudent* stu3 = [[TRStudent alloc]init];
- stu3.age = 20;
- stu3.name = @"wangwu";
- NSComparisonResult(^compare)(id stu1,id stu2);
- compare = ^(id stu1,id stu2)
- {
- //类型转换
- NSString* s1 = nil;
- if([stu1 isMemberOfClass:[TRStudent class]])
- {
- s1 = ((TRStudent*)stu1).name;
- }
- NSString* s2 = nil;
- if([stu2 isMemberOfClass:[TRStudent class]])
- {
- s2 = ((TRStudent*)stu2).name;
- }
- return [s1 compare:s2];
- };
- NSArray* array = @[stu1,stu2,stu3];
- NSLog(@"array:%@",array);
- NSArray* array2 = [array sortedArrayUsingComparator:compare];
- NSLog(@"array2:%@", array2);
- }
- return 0;
- }
上述代码中,以下代码:
- NSArray* array = @[stu1,stu2,stu3];
- NSLog(@"array:%@",array);
用三个学生对象创建一个数组array。
上述代码中,以下代码:
- NSArray* array2 = [array sortedArrayUsingComparator:compare];
向数组对象array发送NSArray类的方法sortedArrayUsingComparator:消息,使用Block变量compare作为实参,传递一个排序方法,在该消息内对数组内的所有数组元素进行排序,并将排序的结果生成一个新的数组返回。
1.4 完整代码
本案例中,类TRStudent声明,即TRStudent.h文件,完整代码如下所示:
- #import <Foundation/Foundation.h>
- @interface TRStudent : NSObject
- @property(nonatomic,assign)int age;
- @property(nonatomic,copy)NSString* name;
- @end
类TRStudent实现,即TRStudent.m文件,完整代码如下所示:
- #import "TRStudent.h"
- @implementation TRStudent
- -(NSString *)description{
- return [NSString stringWithFormat:@"age:%d name:%@",self.age,self.name];
- }
- @end
主程序,即main.m,完整代码如下所示:
- #import <Foundation/Foundation.h>
- #import "TRStudent.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- // insert code here...
- TRStudent* stu1 = [[TRStudent alloc]init];
- stu1.age = 18;
- stu1.name = @"zhangsan";
- TRStudent* stu2 = [[TRStudent alloc]init];
- stu2.age = 19;
- stu2.name = @"lisi";
- TRStudent* stu3 = [[TRStudent alloc]init];
- stu3.age = 20;
- stu3.name = @"wangwu";
- NSComparisonResult(^compare)(id stu1,id stu2);
- compare = ^(id stu1,id stu2)
- {
- //类型转换
- NSString* s1 = nil;
- if([stu1 isMemberOfClass:[TRStudent class]])
- {
- s1 = ((TRStudent*)stu1).name;
- }
- NSString* s2 = nil;
- if([stu2 isMemberOfClass:[TRStudent class]])
- {
- s2 = ((TRStudent*)stu2).name;
- }
- return [s1 compare:s2];
- };
- NSArray* array = @[stu1,stu2,stu3];
- NSLog(@"array:%@",array);
- NSArray* array2 = [array sortedArrayUsingComparator:compare];
- NSLog(@"array2:%@", array2);
- }
- return 0;
- }
Runtime 、 Block的更多相关文章
- iOS开发——OC基础-ARC、BLOCK、协议
一.ARC ARC 是一种编译器特性!而不是IOS运行时特性,和JAVA中得垃圾回收机制完全不一样ARC是自iOS 5之后增加的新特性,完全消除了手动管理内存的烦琐,编译器会自动在适当的地方插入适当的 ...
- display:inline、block、inline-block 的区别
一.块级元素 与 行级元素 要理解display:inline.block.inline-block的区别,需要先了解HTML中的块级(block)元素和行级(inline)元素的特点,行内元素也叫 ...
- 理解display:inline、block、inline-block
要理解display:inline.block.inline-block的区别,需要先了解HTML中的块级(block)元素和行级(inline)元素的特点,行内元素也叫内联元素. 块级元素 总是另起 ...
- (8/18)重学Standford_iOS7开发_协议、block、动画_课程笔记
第八课: 1.协议 另一种安全处理id类型的方式如:id <MyProtocol> obj a.声明 //协议一般放于.h文件中或者在类的.h文件中 @protocol Foo <X ...
- HTML5 display:inline、block、inline-block的区别--备用
display:block就是将元素显示为块级元素. block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度 <div& ...
- display:inline、block、inline-block的区别(转)
display:block就是将元素显示为块级元素. block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度 <div& ...
- 回传值(代理、通知、block)
回传值问题,一直都是困扰初学者的问题,今写者 代理.通知.block 三者的回传值做了一个小小的总结, Main.storyboard 视图: 通过代码分别创建三个代表 代理.通知.block 的按钮 ...
- 04-Foundation-NSSet、NSDictionary、block
目录: 一.NSSet集合 二.NSDictionary字典 三.block代码块 回到顶部 一.NSSet集合 1 NSSet是一个无序的,管理对个对象的集合类,最大特点是集合中不允许出现重复对象, ...
- 包建强的培训课程(12):iOS深入学习(内存管理、Block和GCD等)
@import url(/css/cuteeditor.css); @import url(http://i.cnblogs.com/Load.ashx?type=style&file=Syn ...
随机推荐
- BZOJ1520 [POI2006]Szk-Schools
裸的费用流啊... 建图:对于一个点p拆成两个p1和p2,S向p1连边,流量为1,费用为0:p2向T连边流量为1,费用为0 然后i1向a2到b2分别连边,不妨设i1向p2连边,流量为1,费用为|i - ...
- Icon资源详解[1]
本文分享&备忘最近了解到的icon资源在windows平台下相关的一部分知识,所有测试代码都尽可能的依赖win32 API实现.更全面的知识,参考文末列出的”参考资料“. 关键字:I ...
- JSP 客户端请求
当浏览器请求一个网页时,它会向网络服务器发送一系列不能被直接读取的信息,因为这些信息是作为HTTP信息头的一部分来传送的.您可以查阅HTTP协议来获得更多的信息. 下表列出了浏览器端信息头的一些重要内 ...
- CSS3学习教程:Media Queries详解
说起CSS3的新特性,就不得不提到 Media Queries . Media Queries 的引入,其作用就是允许添加表达式用以确定媒体的情况,以此来应用不同的样式表.换句话说,其允许我们在不改变 ...
- spring mvc与mybatis收集到博客
mybaits-spring 官方教程 http://mybatis.github.io/spring/zh/ SpringMVC 基础教程 框架分析 http://blog.csdn.net/swi ...
- Oracle ODP.NET 篇
1.C# 使用 System.Data.OracleClient 连接 Oracle 需要安装 instantclient , 并配置相应环境变量.重启,方可使用. 2. 使用 System.Data ...
- Spring对jdbc的支持
Spring对jdbc技术提供了很好的支持. 体现在: 1)Spring对c3p连接池的支持很完善: 2)Spring对jdbc提供了JdbcTemplate,来简化jdbc操作: 1.使用步骤 1) ...
- 戴文的Linux内核专题:07内核配置(3)
转自Linux中国 OK,我们还继续配置内核.还有更多功能等待着去配置. 下一个问题(Enable ELF core dumps (ELF_CORE))询问的是内核是否可以生成内核转储文件.这会使内核 ...
- CSU 1160 A(Contest #3)
Description 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示. Input 每行一个整数x,0<= x <= 2^31. Output 每行输出对应的 ...
- 移动设备和SharePoint 2013 - 第5部分:自定义应用
博客地址:http://blog.csdn.net/foxdave 原文地址 在该系列文章中,作者展示了SharePoint 2013最显著的新功能概观--对移动设备的支持. 该系列文章: 移动设备和 ...