7、block、数组排序2

7、block、数组排序2

Student.h
#import <Foundation/Foundation.h>

@interface Student : NSObject

{
    NSInteger _num;
    NSString *_name;
    NSInteger _age;
    CGFloat _score;
}

- (void)setNum:(NSInteger)num;
- (NSInteger)num;

- (void)setName:(NSString *)name;
- (NSString *)name;

- (void)setAge:(NSInteger)age;
- (NSInteger)age;

- (void)setScore:(CGFloat)score;
- (CGFloat)score;

- (instancetype)initWithNum:(NSInteger)num
                       name:(NSString *)name
                        age:(NSInteger)age
                      score:(CGFloat)score;

+ (instancetype)studentWithNum:(NSInteger)num
                          name:(NSString *)name
                           age:(NSInteger)age
                         score:(CGFloat)score;

@end
Student.m
#import "Student.h"

@implementation Student

- (void)setNum:(NSInteger)num
{
    _num = num;
}
- (NSInteger)num
{
    return _num;
}

- (void)setName:(NSString *)name;
{
    _name = name;
}
- (NSString *)name
{
    return _name;
}

- (void)setAge:(NSInteger)age
{
    _age = age;
}
- (NSInteger)age
{
    return _age;
}

- (void)setScore:(CGFloat)score
{
    _score = score;
}
- (CGFloat)score
{
    return _score;
}

- (instancetype)initWithNum:(NSInteger)num
                       name:(NSString *)name
                        age:(NSInteger)age
                      score:(CGFloat)score
{
    self = [super init];

    if (self) {
        _num = num;
        _name = name;
        _age = age;
        _score = score;
    }

    return self;
}

+ (instancetype)studentWithNum:(NSInteger)num
                          name:(NSString *)name
                           age:(NSInteger)age
                         score:(CGFloat)score
{
    return [[Student alloc] initWithNum:num name:name age:age score:score];
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"num = %ld, name = %@, age = %ld, score = %.2f", _num, _name, _age, _score];
}

@end
main.m
#import <Foundation/Foundation.h>
#import "Student.h"


// 给block起别名
typedef CGFloat (^CalBlock) (CGFloat, CGFloat);


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

        // eg.1 写⼀个 返回值为整型 参数为NSString(仅一个参数)的block,实现将字符串转换为整型的功能。
        int (^block1) (NSString *str) = ^int (NSString *str) {
            return [str intValue];
        };

        // 简写
        int (^block2) (NSString *) = ^(NSString *str) {
            NSLog(@"block内部");
            return [str intValue];
        };

        // 类型:int (^) (NSString *)
        // 变量名:block1

        NSLog(@"block外部");

        // 回调
        // 调用block实现将字符串转换为数字
        int i = block2(@"1234");
        NSLog(@"%d", i);


        // 声明block变量,将浮点型数字变为字符串

        NSString * (^block3) (float) = ^(float f) {
            return [NSString stringWithFormat:@"%.2f", f];
        };

        NSString *str = block3(8188.8);
        NSLog(@"str = %@", str);


        // 实现两个NSInteger的和、差、积、商
        // 和
        NSInteger (^block4) (NSInteger, NSInteger) = ^(NSInteger a, NSInteger b) {
            return a + b;
        };
        NSInteger sum = block4(10, 20);
        NSLog(@"sum = %ld", sum);

        // 差
        NSInteger (^block5) (NSInteger, NSInteger) = ^(NSInteger a, NSInteger b) {
            return a - b;
        };
        NSInteger sub = block5(10, 29);
        NSLog(@"sub = %ld", sub);

        // 积
        NSInteger (^block6) (NSInteger, NSInteger) = ^(NSInteger a, NSInteger b) {
            return a * b;
        };
        NSInteger mul = block6(10, 3);
        NSLog(@"mul = %ld", mul);

        // 商
        CGFloat (^block7) (CGFloat, CGFloat) = ^(CGFloat a, CGFloat b) {
            return a / b;
        };
        CGFloat div = block7(34.9, 12.0);
        NSLog(@"div = %.2f", div);


        // 判断数组中是否存在某个元素
        BOOL (^block0) (NSArray *, NSString *) = ^(NSArray *arr, NSString *str) {
            return [arr containsObject:str];
        };
        BOOL b = block0(@[@"aab", @"bb", @"cc"], @"bb");
        NSLog(@"b = %d", b);


        // 使用新类型定义block变量
        __block int bei = 10;
        CalBlock productBlock = ^(CGFloat f1, CGFloat f2) {
            bei = 100;
            return f1 * f2 * bei;
        };
        CGFloat product = productBlock(1.2, 3.0);
        NSLog(@"%.2f", product);

        // block内部可使用外部局部变量的值,但不能修改,若要修改,需用 __block 修饰变量
        // block可使用和修改全局变量的值


        // 使用block对数组排序

        NSArray *namesArray = @[@"hehe", @"xx", @"jian", @"daozhang", @"qing"];
//
//        NSComparisonResult (^sortBlock) (id, id) = ^(id obj1, id obj2) {
//            // or return [obj1 compare:obj2];
//            if ([obj1 compare:obj2] == NSOrderedDescending) {
//                return -1L;
//            } else {
//                return 1L;
//            }
//        };



//        namesArray = [namesArray sortedArrayUsingComparator:sortBlock];
//        NSLog(@"%@", namesArray);


        namesArray = [namesArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            if ([obj1 compare:obj2] == NSOrderedDescending) {
                return 1l;
            }
            return 0l;
        }];
        NSLog(@"%@", namesArray);

        // 可变数组排序,排序方法没有返回值
        NSMutableArray *numbersArray = [NSMutableArray arrayWithObjects:@"123", @"345", @"234", @"56", @"2356", nil];
        [numbersArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            int a = [obj1 intValue];    // 按整型数字排序
            int b = [obj2 intValue];
            if (a < b) {
                return 1L;
            }
            return 0L;
        }];
        NSLog(@"%@", numbersArray);


        // 创建学生对象
        Student *stu1 = [[Student alloc] initWithNum:1002 name:@"laoda" age:18 score:78.9];
        Student *stu2 = [[Student alloc] initWithNum:1005 name:@"laoer" age:19 score:78.9];
        Student *stu3 = [[Student alloc] initWithNum:1001 name:@"laosan" age:20 score:78.9];
        Student *stu4 = [[Student alloc] initWithNum:1004 name:@"laosi" age:17 score:78.9];
        Student *stu5 = [[Student alloc] initWithNum:1003 name:@"laowu" age:21 score:87.0];

        NSMutableArray *studentArray = [NSMutableArray arrayWithObjects:stu1, stu2, stu3, stu4, stu5, nil];

        [studentArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
//            Student *s1 = obj1;
//            Student *s2 = obj2;
//            if (s1.age < s2.age) {
//                return 1L;
//            }
//            return 0L;
            // or
            if ([obj1 age] < [obj2 age]) {
                return 1L;
            }
            return 0L;
        }];

        NSLog(@"%@", studentArray);

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值