转自:http://blog.csdn.net/qq_31810357
<b></b>
"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型,所以如果是其他类型一定要写上
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];//调用自定义块
return 0;
}
自定义的student类的.h文件和.m文件如下:
.m文件
// student.m
// lesson6
// created by lanou3g on 15-4-7.
@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
欢迎分享本文,转载请注明出处!