1 //2 //main.m3 //actual application of block4 //5 //Created by Longma on 15/9/2.6 //Copyright (c) 2015 Dast. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten #import "Countries.h" One intMainintargcConst Char*argv[]) A { - Char* countries[] = {"Uk","Korean"," China","Japan","Autralia","Vietnam","Germany"}; - intLength =sizeof(countries)/sizeof(countries[0]); the__block unsignedLongNUM1; -__block unsignedLongnum2; - -Countries *C1 = [countriesNew]; + -NSLog (@"The country names are sorted by length as follows:"); +[C1 sortwithcountries:countries andlength:length Withblock:^bool (Char*STR1,Char*str2) A { atNUM1 =strlen (str1); -num2 =strlen (str2); - - //sweat, put subtraction overflow, later unsigned type to do subtraction, remember to use int strong to participate in subtraction operation data. - return(int) NUM1-(int) num2 >0 ; - }]; in - [C1 showcountries:countries withlength:length]; to +NSLog (@"The country names are sorted by dictionary order as follows:"); -[C1 sortwithcountries:countries andlength:length Withblock:^bool (Char*STR1,Char*str2) the { * returnstrcmp (str1, str2) >0; $ }];Panax Notoginseng [C1 showcountries:countries withlength:length]; - the return 0; +}
1 //2 //Countries.h3 4 #import<Foundation/Foundation.h>5typedef BOOL (^cmpblock) (Char*,Char*);6 @interfaceCountries:nsobject7- (void) Sortwithcountries: (Char*[]) Countries Andlength: (int) Length Withblock: (Cmpblock) CMPB;8- (void) Showcountries: (Char*[]) Countries Withlength: (int) length;9 @end
1 //2 //COUNTRIES.M3 4 #import "Countries.h"5 #import<string.h>//don't forget.6 7 @implementationcountries8 9 //char* must be followed by [], is a character pointer array, not a character pointer, and (char*) [] is a pointer to an array, no! Cmpb:block variables, code to pass in the comparison methodTen- (void) Sortwithcountries: (Char*[]) Countries Andlength: (int) Length Withblock: (cmpblock) CMPB One { A for(inti =0; I < length-1; i++)//Bubble Sort - { - for(intj =0; J < Length-1I J + +) the { - //int res = strcmp (Countries[j], countries[j + 1]);//change it here to block . -BOOL res = CMPB (countries[j],countries[j +1]);//The block custom return value is BOOL - if(YES = =Res) + { - Char* Temp =Countries[j]; +COUNTRIES[J] = countries[j +1]; ACountries[j +1] =temp; at } - } - } - } - -- (void) Showcountries: (Char*[]) Countries Withlength: (int) Length in { - for(inti =0; i < length; i++) to { +NSLog (@"%s", Countries[i]); - } the } * @end
The results of the operation are as follows:
Ps:bug analysis. unsigned long type attribute subtraction will overflow the problem, check for a long time, the Deskmate and the counselor are not resolved! The last self-certification to find out is overflow,
In the future to meet this situation, do subtraction before the strong to int to ensure that negative numbers can normally be obtained. Received with the INT type variable or compared with 0 with the BOOL type variable!
The verification diagram is as follows:
Sort country names in different ways (call block variable implementation)