- "code" class="OC">
- //
- // main.m
- // Copyright (c) 2015年 tongxing. All rights reserved.
- //
- #import
- #import "Student.h"
- typedef int(^BlockType)(int,int);
- int globalVariable = 200;
- int main(int argc, const charchar * argv[])
- {
- @autoreleasepool {
- //int (int a,int b)表示匿名函数的类型
- //int (^ )(int a,int b)表示Block的类型
- //myBlock实现的是匿名函数的实现体部分
- int (^ myBlock)(int a ,int b) =^int(int a ,int b){
- return a+b;
- };//这里需要加分号,因为这整个部分相当于一个赋值语句,大括号内容赋值给块变量myBlock
- //按照调用函数的方式调用块对象
- int value = myBlock(3,5);//通过myBlock实现两个整型数的求和,返回值为int
- NSLog(@"%d",value);
- //写⼀个 返回值为整型 参数为NSString(仅⼀一个参数)的block,实现将字符串转换为整型的功能.
- int (^strBlock)(NSString *str)=^int(NSString *str){
- int value =[str intValue];
- return value;
- };
- NSString *str = @"123";
- //使用block
- NSLog(@"%d",strBlock(str));
- //练习,定义三个block求商/差/积
- int (^cutBlock)(int ,int )=^int(int a,int b){
- return a-b;
- };
- int (^dealerBlock)(int ,int)= ^(int a,int b){
- return a%b;
- };
- int (^rideBlock)(int,int ) = ^(int a,int b){
- return a*b;
- };
- NSLog(@"%d",cutBlock(3,5));
- NSLog(@"%d", dealerBlock(3,5));
- NSLog(@"%d",rideBlock(3,5));
- //对于上面三个函数的类型都一样,所以可以引入typedef来在#import下面进行重定义
- #pragma mark----给相同block类型进行重命名
- BlockType myBlock1 = ^int(int a,int b){//对于^后面的int可以省略,但是系统默认的是int型,所以如果是其他类型一定要写上
- return a-b;
- };
- NSLog(@"%d",myBlock1(4,5));
- //.....
- #pragma mark----block与全局变量
- //定义一个全局变量globalVariable
- void (^block)(void) = ^(){//^(void)(),前面的返回值类型void可以省略,但是后面的括号语法上定义是不可以省略(),但是经过测试没有参数也可以省略
- globalVariable++;//如果是全局变量可以直接被访问和修改
- NSLog(@"%d",globalVariable);
- };
- block();
- #pragma mark----block与局部变量
- // int number =1;//局部变量需要加__block,否则会提示出错
- __block int number = 1;
- void (^block1)(void) = ^(){
- number++;//如果是局部变量不可以直接被访问和修改,必须在number前面加一个__block,这里需要打两个小_
- NSLog(@"%d",number);
- };
- block1();
- #pragma mark----block自动截取变量
- int val = 10;
- void (^blk)(void) = ^(){
- // printf("val=%d\n",val);
- NSLog(@"%d",val);
- };
- val = 2;
- blk();
- //上面这段代码,输出值是:val = 10.而不是2.block截获自动变量的瞬时值。因为block保存了自动变量的值,所以在执行block语法后,即使改写block中使用的自动变量的值也不会影响block执行时自动变量的值。
- #pragma mark----block与数组排序
- //关于数组的排序已经总结过,这里做一个比较
- //使用系统自带compare比较系统自带对象
- NSArray *strsArray =[NSArray arrayWithObjects:@"abc",@"cdf",@"ade",@"feg", nil nil];
- NSArray *newArray = [strsArray sortedArrayUsingSelector:@selector(compare:)];
- NSLog(@"%@",newArray);
- //使用block方式比较系统自带对象
- //该种方式是按block的语法按步骤来进行比较的,下面背注掉的是使用简便的方式,两者原理一样
- NSComparisonResult(^strBlock1)(id obj1,id obj2)=^NSComparisonResult(id obj1,id obj2){
- NSString *result1 = (NSString *)obj1;
- NSString *result2 = (NSString *)obj2;
- return [result1 compare:result2];
- };
- NSArray *resut3 = [strsArray sortedArrayUsingComparator:strBlock1];//strBlock1
- NSLog(@"%@--",resut3);
- // NSArray *sortArray = [strsArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {^NSComparisonResult(id obj1, id obj2)相当于上面的strBlock1
- // NSString *result1 = (NSString *)obj1;
- // NSString *result2 = (NSString *)obj2;
- // return [result1 compare:result2];
- // }];
- // NSLog(@"%@",sortArray);
- //对于系统自带的对象和类都可以用compare方法,但是自定义的类需要自己重写compare方法
- //使用自定义compare方法对数组中得对象按照年龄进行排序,(数组中的对象为自定义类型的对象)
- Student *stu1 = [Student studentWithName:@"刘亦菲" Age:26 Score:89.0];
- Student *stu2 = [Student studentWithName:@"张飞" Age:34 Score:78.0];
- Student *stu3 = [Student studentWithName:@"周冬雨" Age:22 Score:85.0];
- NSMutableArray *stusarray = [NSMutableArray arrayWithObjects:stu1,stu2,stu3 , nil nil];
- [stusarray sortUsingSelector:@selector(studentCompare:)];
- for (NSString * str in stusarray) {
- NSLog(@"%@",str );
- }
- //使用block方式对数组中得对象按照年龄进行排序,(数组中的对象为自定义类型的对象)
- [stusarray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
- Student *stu1 = (Student *)obj1;//强制转换成自定义类型
- Student *stu2 = (Student *)obj2;
- return [stu1 studentCompare:stu2];
- }];
- for (NSString *stri in stusarray) {
- NSLog(@"%@",stri);
- }
- //使用自定义的block实现对数组中的对象进行排序,基本处理方式与上述的处理系统对象相似,只是如果不知道参数类型必须进行强制转换
- NSComparisonResult(^stuBlock)(id ob1,id ob2) =^ NSComparisonResult(id ob1,id ob2){
- Student *stud1 = (Student *)ob1;//强制转换
- Student *stud2 = (Student *)ob2;
- return [stud1 studentCompare:stud2];
- };
- [stusarray sortUsingComparator:stuBlock];//调用自定义块
- for (NSString *stri in stusarray) {
- NSLog(@"%@",stri);
- }
- }
- return 0;
- }
自定义的Student类的.h文件和.m文件如下:
.h文件 // // Student.h // Copyright (c) 2015年 tongxing. All rights reserved. // #import @interface Student : NSObject @property NSString *name; @property int age; @property float score; //这里提前使用了OC中得属性 //关于属性的知识,后面将会给出相关学习,有兴趣的同学可以去博客园了解:http://www.cnblogs.com/wendingding/p/3706430.html //{ // NSString *_name; // int _age; // float _score; //} //-(void)setName:(NSString *)name; //-(void)setAge:(int)age; //-(void)setScore:(float)score; //-(float)score; //-(NSString *)name; //-(int)age; -(id)initWithName:(NSString *)name Age:(int)age Score:(float)score; +(id)studentWithName:(NSString *)name Age:(int)age Score:(float)score; //自定义比较方法 -(NSComparisonResult)studentCompare:(Student *)student; @end
- .m文件
- //
- // Student.m
- // lesson6
- //
- // Created by lanou3g on 15-4-7.
- // Copyright (c) 2015年 tongxing. All rights reserved.
- //
- #import "Student.h"
- @implementation Student
- //@synthesize _name ;
- //@synthesize _age;
- //@synthesize _score ;
- //自从xcode 4.4后,@property就独揽了@property和@synthesize的功能。
- //-(void)setName:(NSString *)name{
- // _name = name;
- //}
- //-(void)setAge:(int)age{
- // _age = age;
- //}
- //-(NSString *)name{
- // return _name;
- //}
- //-(int)age{
- // return _age;
- //}
- //-(void)setScore:(float)score{
- // _score = score;
- //}
- //-(float)score{
- // return _score;
- //}
- -(id)initWithName:(NSString *)name Age:(int)age Score:(float)score{
- if (self = [super init]) {
- _name = name;
- _age = age;
- _score = score;
- }
- return self;
- }
- +(id)studentWithName:(NSString *)name Age:(int)age Score:(float)score{
- Student *student = [[Student alloc]initWithName:name Age:age Score:score];
- return student;
- }
- -(NSComparisonResult)studentCompare:(Student *)student{
- NSComparisonResult result1 = [[NSNumber numberWithInt:self.age] compare:[NSNumber numberWithInt:student.age]];
- return result1;
- }
- -(NSString *)description{
- return [NSString stringWithFormat:@"%@,%d",_name,_age];
- }
- @end
- 欢迎分享本文,转载请注明出处!
-