標籤:objective block 數組排序 oc objetive-c
直接上代碼:
//寫?一個 傳回值為整型 參數為NSString(僅?一個參// 數)的block,實現將字串轉換為整型的功能。// int (NSString *string) {// return [string intValue] ;// }// 把^int (NSString *string)賦值給int (^myBlock)(NSString *)// ^int (NSString *string)是一個參數為NSString *類型、傳回值為int類型的匿名函數// 可定義在main()函數外面// block的資料類型代表了匿名函數的格式(傳回值類型,形參類型)// block變數的定義與函數指標變數的定義類似,唯一區別於函數指標變數的是變數名前通過脫字元‘ ^ ’修飾。// 首先應該用 ^ 修飾,剩餘的部分與C語言函數定義一致,最大的不同就是沒有函數名(同時傳回值類型也可以省略)// block變數在定義時具有變數定義的基本特徵,賦值號右側的匿名函數可以當做一個整體被賦值,類似於 int a = 5 ;// block變數所賦值的值是匿名函數,又兼具函數的特徵,並且是唯一可以定義在某個函數實現內部的(C語言中認為函數是不能嵌套定義的,block是個特例) int (^myBlock)(NSString *) = ^int (NSString *string) { return [string intValue] ; } ; NSLog( @"%d", myBlock(@"123") + 1 ) ;// int (^sumBlock)( int, int ) = ^( int number1, int number2) 這樣也OK int (^sumBlock)( int, int ) = ^int ( int number1, int number2) { return number1 + number2 ; } ; NSLog( @"%d", sumBlock( sumBlock( 1, 2), sumBlock( 3, 4 ) ) ) ;// 類比函數指標的類型定義,格式與函數指標一致,類型定義一定程度上簡化了block的使用// typedef int (^BlockType)( int, int ) ; BlockType sum = ^int ( int n, int m ) { return m + n ; } ; NSLog( @"%d", sum( 2, 3) ) ;// __block表示其所定義的變數可以在block函數體內使用;// __block類型標示符可以允許局部變數在其後續定義的block內部正常訪問// (注意:把__block int count = 0 ;不能定義在__block函數體後面) __block int count = 8 ; void (^testBlock)() = ^() {// int count = 9 ;// __block int count = 6 ; // XXXX cuo wu for (int i = 0; i < 10; i++ ) { count++ ; NSLog( @"%d", count ) ; } } ;// __block int count = 8 ;// NSLog( @"%d", count ) ; //block調用 testBlock() ; NSComparisonResult (^comareResult)(NSString *, NSString *) = ^(NSString * str1, NSString *str2) { return [str1 compare:str2] ; } ; NSLog( @"%ld", comareResult( @"456", @"123") ) ; /* * 數組排序 */ NSArray *array= @[@1, @3, @2, @5, @4] ; NSArray *resultArray1 = [array sortedArrayUsingSelector:@selector(compare:)] ; NSLog( @"%@", resultArray1 ) ; __block int number = 0 ; NSComparator sortBlock = ^( id obj1, id obj2 ) { number++; return [obj2 compare:obj1] ; } ; NSLog( @"%d", number ) ;//先執行 NSArray *resultArray2 = [array sortedArrayUsingComparator:sortBlock] ; NSLog( @"%d", number ) ; NSLog( @"%@", resultArray2 ) ; //也可以這麼寫 //NSArray *resultArray3 = [array sortedArrayUsingComparator:^(id obj1, id obj2) NSArray *resultArray3 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [obj2 compare:obj1] ; }] ; NSLog( @"%@", resultArray3 ) ; /* * 練習二、 * * 對上例數組 * 1、按學號升序排列 * 2、按學號降序排列 * 3、按姓名升序排列 * 4、按姓名降序排列 */ Student *stu1 = [Student studentWithName:@"wang" number:1001 age:23] ; Student *stu2 = [Student studentWithName:@"zhen" number:1003 age:22] ; Student *stu3 = [Student studentWithName:@"gang" number:1002 age:24] ; NSArray *strArray = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil] ; NSLog( @"%@", [strArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [[obj1 getName] compare:[obj2 getName]] ; }] ) ; NSLog( @"%@", [strArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [[NSString stringWithFormat:@"%d", [obj1 getNumber]] compare:[NSString stringWithFormat:@"%d", [obj2 getNumber]]] ; }] ) ; NSLog( @"%@", [strArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [[NSString stringWithFormat:@"%d", [obj1 getAge]] compare:[NSString stringWithFormat:@"%d", [obj2 getAge]]] ; }] ) ; NSMutableArray *stus = [NSMutableArray array] ; NSArray *names = @[@"Duke", @"Douglas", @"Allen", @"Lily", @"Lucy", @"Jack", @"Rose", @"Jeams", @"Tom", @"Frank"] ; for ( int i = 0; i < 10; i++ ) { Student *stu = [Student studentWithName:names[i] number:arc4random() % 9000 + 1000 age:arc4random() % 100] ; [stus addObject:stu] ; } NSLog( @"%@", stus ) ; //注意:區別:[strArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) [stus sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {// return [[obj1 getName] compare:[obj2 getName]] ; NSInteger age1 = [obj1 getAge] ; NSInteger age2 = [obj2 getAge] ; if (age1 < age2) { return NSOrderedDescending ; } else if (age1 > age2) { return NSOrderedAscending ; } return NSOrderedSame ; }] ; NSLog(@"%@",stus) ;
Student.h 檔案:
//// Student.h//// Created by on 15/4/7.// Copyright (c) 2015年 . All rights reserved.//#import <Foundation/Foundation.h>@interface Student : NSObject{ NSString *_name ; // 姓名 int _number ; // 學號 int _age ; // 年齡 @protected NSString *_a ; @private NSString *_b ;}- (instancetype)initWithName:(NSString *)name number:(int)number age:(int)age ;+ (instancetype)studentWithName:(NSString *)name number:(int)number age:(int)age ;- (NSString *)getName ;- (int)getNumber ;- (int)getAge ;@end // Student
Student.m 檔案:
//// Student.m//// Created by on 15/4/7.// Copyright (c) 2015年 . All rights reserved.//#import "Student.h"@implementation Student- (instancetype)initWithName:(NSString *)name number:(int)number age:(int)age { if ( self = [super init] ) { _name = name ; _number = number ; _age = age ; } return self ;}+ (instancetype)studentWithName:(NSString *)name number:(int)number age:(int)age { return [[Student alloc] initWithName:name number:number age:age] ;}- (NSString *)getName { return _name ;}- (int)getNumber { return _number ;}- (int)getAge { return _age ;}- (NSString *)description { return [[NSString alloc] initWithFormat:@"%@ %d %d", _name , _number , _age ] ;}@end // Student
Objective-C----Block 、數組排序