ios 开发 OC编程 块语法bolck

//

//  main.m

//  oc- 07 block

//

//  Created by dllo on 15/11/3.

//  Copyright (c) 2015 dllo. All rights reserved.

//

//void test(void)

//{

//    printf("hanshu");

//}

#import <Foundation/Foundation.h>

#import "Student.h"


NSInteger glB = 5;

//全局变量的声明 1, 全部大写 2, gl


typedef void (^blockType)(void);

int main(int argc, const char * argv[]) {

//    void (*p)(void) = test;

    //block语法

    //block1- block名字

    //block的实现在方法内部

//    void (^block1)(void) = ^(void) {

//        NSLog(@"block1");

//    };

//    

    //调用

   // block1();

    //练习1 创建block,实现打印指定次数的block2

    //练习2 创建block,分别实现加减乘除

    //练习3 创建block, 分别判断某个数字是否为偶数

    //练习4 写一个返回值为整形,参数为NSString(仅一个参数)block,实现将字符型转换为整形的功能

    //提高题,创建学生类,分别编写block判断2名学生的分数,年龄,姓名的大小

    //1

//    void(^block)(NSInteger a)= ^(NSInteger a) {

//        for (NSInteger i = 0; i < a; i++) {

//            NSLog(@"block2");

//

//        }

//      

//    };

//    block(4);

    //2

//    int(^block2)(int a, int b) = ^(int a, int b) {

//        return a + b;

//    };

//    

//    int(^block3)(int a, int b) = ^(int a, int b) {

//        return a - b;

//    };

//    int(^block4)(int a, int b) = ^(int a, int b) {

//        return a  * b;

//    };

//    int(^block5)(int a, int b) = ^(int a, int b) {

//        return a / b;

//    };

//   int  ret = block2(4,3);

//    NSLog(@"%d", ret);

//    int  ret1 = block3(4,3);

//    NSLog(@"%d", ret1);

//    int  ret2 = block4(4,3);

//    NSLog(@"%d", ret2);

//    int  ret3 = block5(4,3);

//    NSLog(@"%d", ret3);

//    //3

//    BOOL(^block6)(int a) = ^(int a) {

//        if (0 == a % 2) {

//            BOOL ret = YES;

//            return ret;

//        } else

//            return NO;

//        

//    };

//    NSLog(@"%d", block6(4));

//    //4

//    NSInteger(^block7)(NSString *) = ^(NSString *a) {

//        NSInteger b = [a intValue];

//        return b;

//    };

//    NSInteger b = block7(@"234");

//    NSLog(@"%ld",b);

    //提高

//    Student *stu1 = [[Student alloc]initWithName:@"yuhao" age:21 score:99];

//    Student *stu2 = [[Student alloc]initWithName:@"laoda" age:23 score:59];

//    Student *stu3 = [[Student alloc]initWithName:@"zy" age:22 score:56];

//    NSMutableArray *arr = [NSMutableArray arrayWithObjects:stu1, stu2, stu3, nil];

//    


//    NSComparisonResult(^compareName)(NSString *, NSString *) = ^(NSString *a, NSString *b) {

//        return [a compare:b];

//    };

//    compareName(stu1.name, stu2.name);

//    NSComparisonResult(^compareAge)(NSInteger, NSInteger)= ^(NSInteger a, NSInteger b) {

//        if (a > b) {

//            

//            return NSOrderedAscending;

//        if (a < b) {

//                return NSOrderedDescending;

//            }

//        return NSOrderedSame;

//        

//

//        }

//    };

//    

//    compareAge(stu1.age, stu2.age);

//    void(^comparescore)(NSInteger,NSInteger) = ^(NSInteger a, NSInteger b) {

//        NSLog(@"较大值是%ld", a > b ? a : b);

//    };

//    comparescore(stu1.score, stu2.score);

    

    //初级版本

//    NSComparisonResult(^comparName)(id, id) = ^NSComparisonResult(id s1, id s2 ) {

//        Student *stu1 = (Student *)s1;

//        Student *stu2 = (Student *)s2;

//        return [stu1.name compare: stu2.name];

//        

//    };

  //  [arr sortUsingComparator:comparName];

    //究极版本 执行后的数组已经按照要求排好序

    // 比较名字

//    [arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {

//        Student *stu1 = (Student *)obj1;

//        Student *stu2 = (Student *)obj2;

//        return [stu1.name compare:stu2.name];

//        

//    }];

    

    //比较分数

//    [arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {

//        Student *stu1 = (Student *)obj1;

//        Student *stu2 = (Student *)obj2;

//        if (stu1.score > stu2.score) {

//            return NSOrderedAscending ;

//

//        }

//    if (stu1.score < stu2.score) {

//        return NSOrderedDescending;

//    }

//    return NSOrderedSame;

//        

//    }];

//    for (Student *a in arr) {

//        NSLog(@"%@ %ld %ld",[a name], [a age], [a score] );

//    }

    //比较年纪(升序)

//    [arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {

//        Student *st1 = (Student *)obj1;

//        Student *st2= (Student *)obj2;

//        if (st1.age < st2.age) {

//            return NSOrderedAscending;

//        }

//        if (st1.age > st2.age) {

//            return NSOrderedDescending;

//        }

//        return NSOrderedSame;

//        

//    }];

//    for (Student *c in arr) {

//        NSLog(@"%@ %ld %ld", [c name], [c age], [c score]);

//    }

    //对上面的数组1 按照年纪升序排列, 按照年纪降序.按照姓名降序 ,按照姓名升序

    


    //年纪降序

//    [arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {

//        Student *st1 = (Student *)obj1;

//        Student *st2 = (Student *)obj2;

//        if(st1.age < st2.age) {

//            return NSOrderedDescending;

//        }

//        if(st1.age > st2.age) {

//            return NSOrderedAscending;

//        }

//        return NSOrderedSame;

//        

//    }];

//    for (Student *c in arr) {

//        NSLog(@"%@ %ld %ld", [c name], [c age], [c score]);

//    }

    //名字升序

//    [arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {

//                Student *st1 = (Student *)obj1;

//                Student *st2= (Student *)obj2;

//            return [st1.name compare:st2.name];

//        }];

//            for (Student *c in arr) {

//                NSLog(@"%@ %ld %ld", [c name], [c age], [c score]);

//            }

    //名字降序

//    [arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {

//                        Student *st1 = (Student *)obj1;

//                        Student *st2= (Student *)obj2;

//                    return -[st1.name compare:st2.name];

//                }];

//                    for (Student *c in arr) {

//                        NSLog(@"%@ %ld %ld", [c name], [c age], [c score]);

//                    }

    

    // block与变量

    //block 与局部变量: 1, 只读局部变量; 2, 若想修改,可在局部变量定义处加__block声明

    //block与全局变量: 1, 可以读可以写, 可运算

    __block NSInteger a = 3;

    blockType blocktest = ^(void) {

        glB = 4;

        a = 5;

        NSLog(@"%ld",a);

        NSLog(@"%ld", glB);

    };

    blockTest();

    return 0;

}

//

//  Student.h

//  oc- 07 block

//

//  Created by dllo on 15/11/3.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Student : NSObject

@property (nonatomic, copy) NSString *name;

@property (nonatomic, assign) NSInteger age;

@property (nonatomic, assign)NSInteger score;

- (id)initWithName:(NSString *)name age:(NSInteger)age score:(NSInteger)score;

@end


//

//  Student.m

//  oc- 07 block

//

//  Created by dllo on 15/11/3.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "Student.h"


@implementation Student

- (id)initWithName:(NSString *)name age:(NSInteger)age score:(NSInteger)score

{

    self = [super self];

    if (self) {

        [self setName:name];

        [self setAge:age];

        [self setScore:score];

    }

    return self;

}

@end



posted @ 2015-11-08 14:01  挽月细数风流  阅读(291)  评论(0编辑  收藏  举报