在IOS軟件開發(fā)中我們需要依據(jù)NSArray中對象的成員進(jìn)行排序,我們知道對象中成員比較多,因此如果我們寫了多種方法根據(jù)不同的成員進(jìn)行排序,代碼量很大,實(shí)現(xiàn)起來不是很方便,因此蘋果向我們提供了一種快速排序的方法,叫做
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),云浮企業(yè)網(wǎng)站建設(shè),云浮品牌網(wǎng)站建設(shè),網(wǎng)站定制,云浮網(wǎng)站建設(shè)報(bào)價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,云浮網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
- (void)sortUsingSelector:(SEL)comparator;
本文重視講訴原理:
我們先來看一個例子:
#import//Dog頭文件 @interface Dog : NSObject { NSString * _name; NSUInteger _age; } -(NSComparisonResult)nameCompae:(Dog *)dog; -(NSComparisonResult)ageCompae:(Dog *)dog; -(id)initWithName:(NSString *)name andWithAge:(NSUInteger)age; @property (copy,readwrite,nonatomic) NSString * name; @property (assign,readwrite,nonatomic)NSUInteger age; @end //Dog函數(shù)實(shí)現(xiàn)體 @implementation Dog -(NSComparisonResult)nameCompae:(Dog *)dog { if ([_name compare:[dog name]]>0) { return NSOrderedDescending; } return NSOrderedAscending; } -(NSComparisonResult)ageCompae:(Dog *)dog { NSLog(@"%ld %ld",_age,[dog age]); if (_age > [dog age]) { return NSOrderedAscending; } return NSOrderedDescending; } -(void)setAge:(NSUInteger)age { _age = age; NSLog(@"dasdsd"); } -(id)initWithName:(NSString *)name andWithAge:(NSUInteger)age { if (self = [super init]) { _name = name; _age = age; } return self; } @end //main函數(shù) int main(int argc, const char * argv[]) { @autoreleasepool { NSMutableArray * dogArray = [[NSMutableArray alloc]init]; Dog * dog1 = [[Dog alloc]initWithName:@"xiaobao" andWithAge:12]; Dog * dog2 = [[Dog alloc]initWithName:@"zhansan" andWithAge:8]; Dog * dog3 = [[Dog alloc]initWithName:@"lisi" andWithAge:16]; Dog * dog4 = [[Dog alloc]initWithName:@"wangwu" andWithAge:7]; [dogArray addObject:dog1]; [dogArray addObject:dog2]; [dogArray addObject:dog3]; [dogArray addObject:dog4]; for (Dog * temp in dogArray) { NSLog(@"%@ %ld",temp.name,temp.age); } [dogArray sortUsingSelector:@selector(nameCompae:)]; for (Dog * temp in dogArray) { NSLog(@"%@ %ld",temp.name,temp.age); } [dogArray sortUsingSelector:@selector(ageCompae:)]; for (Dog * temp in dogArray) { NSLog(@"%@ %ld",temp.name,temp.age); } } return 0; }
在上面的這個例子中,我將小狗對象存入dogArray中,而該對象中又有兩個成員變量,一個是狗的名字,另一個是狗的年齡,我們通過這兩個屬性對這個dogArray中得對象進(jìn)行排序,在測試中我發(fā)現(xiàn)在調(diào)用sortUsingSelector函數(shù)時,選擇器(即ageCompae:和nameCompae)是被多次調(diào)用的,因此我猜測,該函數(shù)的偽代碼如下:
- (void)mysortUsingSelector:(SEL)comparator:①
{
int temp = 0;
for (int i=0; i<[self count]-1; i++)②
{ temp = i;
for (int j=i+1; j<[self count]; j++)
{
if ([[self objectAtIndex:temp] comparator:[self objectAtIndex:j]]==NSOrderedDescending)③
{
temp = j;
}
}
if (i!=temp)
{
[dogArray exchangeObjectAtIndex:i withObjectAtIndex:temp];
}
}
}
上述代碼中又三點(diǎn)需要理解,只要理解了這個偽代碼,你就知道sortUsingSelector這個函數(shù)是怎么工作的了.
①:(SEL)comparator:這個東西其實(shí)就是模仿C語言中的函數(shù)指針,我們想要使用什么函數(shù)對NSArray的對象只要把函數(shù)入口地址傳給它就好了,例如上述代碼中想使用dog名字進(jìn)行排序,只要把nameCompae:傳給它就好了,而想要按照年齡排序就傳ageCompare:如果你理解函數(shù)指針,這個對你就很簡單了,呵呵
②:這個self其實(shí)就是數(shù)組的地址,對應(yīng)與上面代碼中的dogArray
③:使用傳遞過來的函數(shù)進(jìn)行比較,也就是C語言中函數(shù)指針的回調(diào)函數(shù),上述代碼中有一點(diǎn)很重要,也就是
if ([[self objectAtIndex:temp] comparator:[self objectAtIndex:j]]==NSOrderedDescending)這有在這個語句為真的時候才會進(jìn)行交換,我測試多次sortUsingSelector這個函數(shù)在回調(diào)(SEL)comparator:函數(shù)時,只有在(SEL)comparator:函數(shù)返回值為NSOrderedDescending:才會進(jìn)行排序,對于其它值則不會進(jìn)行如何處理,這也很好的解釋了我上面的if語句的書寫方式是正確的.
總結(jié):相信到了這里你一定明白了sortUsingSelector的工作方式了,如果不明白建議看看關(guān)于函數(shù)指針的視頻或者書籍,推薦在51CTO上面找找;