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"
typedef CGFloat (^CalBlock) (CGFloat, CGFloat);
int main(int argc, const char * argv[]) {
@autoreleasepool {
int (^block1) (NSString *str) = ^int (NSString *str) {
return [str intValue];
};
int (^block2) (NSString *) = ^(NSString *str) {
NSLog(@"block内部");
return [str intValue];
};
NSLog(@"block外部");
int i = block2(@"1234");
NSLog(@"%d", i);
NSString * (^block3) (float) = ^(float f) {
return [NSString stringWithFormat:@"%.2f", f];
};
NSString *str = block3(8188.8);
NSLog(@"str = %@", str);
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 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);
NSArray *namesArray = @[@"hehe", @"xx", @"jian", @"daozhang", @"qing"];
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) {
if ([obj1 age] < [obj2 age]) {
return 1L;
}
return 0L;
}];
NSLog(@"%@", studentArray);
}
return 0;
}