2016-11-13 69 views
0

我是一名初學者程序員,我被賦予了編寫NSMutableArray類別擴展的任務。在谷歌搜索它,我發現很少的信息,並不明白如何做到這一點。 我需要寫一個擴展到Shell排序。我可以使用NSSortDescriptor,但擴展應該給排序算法,標準的NSSortDescriptor方法不適用於此。如果有某種普通用戶關聯引用以及如何使用它,那真的很有幫助。因爲,我發現 - 不適合我。類別NSMutableArray的擴展Objective-C

+2

你能分享你所擁有的代碼那麼遠?你的問題措辭的方式非常廣泛。 – Wellspring

+0

你確切的問題是什麼?它是擴展部分(在NSMutableArray上創建一個類別)還是Shell排序部分? – Larme

回答

0

所以這裏...

的NSMutableArray + Sort.h

#import <Foundation/Foundation.h> 
@interface NSMutableArray (Sort) 

    -(void)sortArrayUsingShellSort; 

@end 

的NSMutableArray + Sort.m

#import "NSMutableArray+Sort.h" 
@implementation NSMutableArray (Sort) 

    -(void)sortArrayUsingShellSort 
    { 
     // tasks 
     // #1. access the unsorted array using self 
     // #2. sort the array using your shell sort, no one gonna do that for you, you have to do that yourself. Re-arrange the items or the array. 

    } 

@end 

一些有用的鏈接,希爾排序

  1. Tutorials point
  2. interactivepuython
  3. Wikipedia
  4. Toptal

進入你的 的main.m

#import <Foundation/Foundation.h> // not necessary actually, as it is already imported into our NSMutableArray+Sort.h 
#import "NSMutableArray+Sort.h" 

int main(int argc, char * argv[]) { 

    NSMutableArray *myArray = [[NSMutableArray alloc] initWithArray:@[ 
              [NSNumber numberWithInt:10], 
              [NSNumber numberWithInt:3], 
              [NSNumber numberWithInt:4], 
              [NSNumber numberWithInt:15] 
              ]]; 


    NSLog(@"array before sorting %@", myArray); 

    //here goes your sorting category function call 
    [myArray sortArrayUsingShellSort]; 

    NSLog(@"array after sorting %@", myArray); 
} 
+0

'UITabBarItem'?並根據每個問題使它適合'NSMutableArray'也許? – CRD

+0

Ooooh。如何在main中使用這個擴展? –

+0

這是一個打字@CRD,你只需要導入「NSMutableArray + Sort.h」,然後調用它的功能,沒有其他的@ owner –