2013-12-09 50 views
-2

我有這樣的代碼將圖像保存在我的應用程序排序在NSHomeDirectory

NSString *fileName = [NSString stringWithFormat:@"2013_%d_a_%d",count,indexToInsert]; 
     NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:fileName]]; 
     NSData *imageData = UIImagePNGRepresentation(imageToAdd); 
     [imageData writeToFile:pngPath atomically:YES]; 
在我的日誌

我看到這一點:

"2013_10_a_1", 
    "2013_1_a_1", 
    "2013_2_a_1", 
    "2013_3_a_1", 
    "2013_4_a_1", 
    "2013_5_a_1", 
    "2013_6_a_1", 
    "2013_7_a_1", 
    "2013_8_a_1", 
    "2013_9_a_1" 

爲什麼「2013_10_1」是在頂部?它位於位置0,我想要位置9(10個元素)

回答

0

這裏的問題是下劃線字符_ascii code 95)在任何數字字符後面排序(ASCII碼48到57)。

更改輸出文件名,包括前導零,你會不會需要惹排序問題:

NSString *fileName = [NSString stringWithFormat:@"2013_%03d_a_%d",count,indexToInsert]; 

將輸出:

"2013_001_a_1", 
"2013_002_a_1", 
"2013_003_a_1", 
"2013_004_a_1", 
"2013_005_a_1", 
"2013_006_a_1", 
"2013_007_a_1", 
"2013_008_a_1", 
"2013_009_a_1", 
"2013_010_a_1" 
+0

啊!!!!!!!!! ! – CrazyDev

0

您的字符串包含數字,因此您需要執行數字排序,而不是純字符串排序。爲此,請使用NSString上的compare:options:方法,並使用NSNumericSearch的選項。