您好,登錄后才能下訂單哦!
在IOS軟件開發(fā)中我們需要依據(jù)NSArray中對(duì)象的成員進(jìn)行排序,我們知道對(duì)象中成員比較多,因此如果我們寫了多種方法根據(jù)不同的成員進(jìn)行排序,代碼量很大,實(shí)現(xiàn)起來不是很方便,因此蘋果向我們提供了一種快速排序的方法,叫做
- (void)sortUsingSelector:(SEL)comparator;
本文重視講訴原理:
我們先來看一個(gè)例子:
#import <Foundation/Foundation.h> //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; }
在上面的這個(gè)例子中,我將小狗對(duì)象存入dogArray中,而該對(duì)象中又有兩個(gè)成員變量,一個(gè)是狗的名字,另一個(gè)是狗的年齡,我們通過這兩個(gè)屬性對(duì)這個(gè)dogArray中得對(duì)象進(jìn)行排序,在測(cè)試中我發(fā)現(xiàn)在調(diào)用sortUsingSelector函數(shù)時(shí),選擇器(即ageCompae:和nameCompae)是被多次調(diào)用的,因此我猜測(cè),該函數(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)需要理解,只要理解了這個(gè)偽代碼,你就知道sortUsingSelector這個(gè)函數(shù)是怎么工作的了.
①:(SEL)comparator:這個(gè)東西其實(shí)就是模仿C語言中的函數(shù)指針,我們想要使用什么函數(shù)對(duì)NSArray的對(duì)象只要把函數(shù)入口地址傳給它就好了,例如上述代碼中想使用dog名字進(jìn)行排序,只要把nameCompae:傳給它就好了,而想要按照年齡排序就傳ageCompare:如果你理解函數(shù)指針,這個(gè)對(duì)你就很簡單了,呵呵
②:這個(gè)self其實(shí)就是數(shù)組的地址,對(duì)應(yīng)與上面代碼中的dogArray
③:使用傳遞過來的函數(shù)進(jìn)行比較,也就是C語言中函數(shù)指針的回調(diào)函數(shù),上述代碼中有一點(diǎn)很重要,也就是
if ([[self objectAtIndex:temp] comparator:[self objectAtIndex:j]]==NSOrderedDescending)這有在這個(gè)語句為真的時(shí)候才會(huì)進(jìn)行交換,我測(cè)試多次sortUsingSelector這個(gè)函數(shù)在回調(diào)(SEL)comparator:函數(shù)時(shí),只有在(SEL)comparator:函數(shù)返回值為NSOrderedDescending:才會(huì)進(jìn)行排序,對(duì)于其它值則不會(huì)進(jìn)行如何處理,這也很好的解釋了我上面的if語句的書寫方式是正確的.
總結(jié):相信到了這里你一定明白了sortUsingSelector的工作方式了,如果不明白建議看看關(guān)于函數(shù)指針的視頻或者書籍,推薦在51CTO上面找找;
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。