2011-12-30 147 views
4

我有一個NSString,我想用數字搜索。看來NSNumericSearch會是合適的解決方案,但顯然只是比較:,caseInsensitiveCompare:,localizedCompare:和localizedCaseInsensitiveCompare:在iOS中執行提取操作時用作排序選擇器。 (這是一個核心數據項目)。我的理解是,這與SQLite後備數據存儲有關。iOS的NSNumericSearch解決方案

我已經嘗試將字符串投射到NSNumber。 ARC有一個錯誤,因爲NSNumber基本上是一個int。

總之,我想對數字進行排序(1,2,11,25),而不是(1,11,2,25)。我會很感激任何解決方案。

編輯:(有急事我對上面的道歉我是從我當時的電腦了,和。)

對有限種類的描述符的信息來自的Isted &哈靈頓2011年的最後一頁「iOS的核心數據」。

When doing a fetch with sort descriptors you'll need to restrict yourself to the following selectors on iOS: compare:, caseInsensitiveCompare:, localizedCompare:, localizedCaseInsensitiveCompare:.

爲了澄清,我寫了這樣的分類:

@interface NSString (SizeComparison) 
-(NSComparisonResult)sizeCompare:(NSString*)otherString; 
@end 
@implementation NSString (SizeComparison) 
-(NSComparisonResult)sizeCompare:(NSString*)otherString { 
return [self compare:otherString options:NSNumericSearch]; 
} 
@end 

在fetchedResultsCntroller排序描述如下:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"size" ascending:YES selector:@selector(sizeCompare:)]; 

它返回此錯誤:

*終止應用程序由於未捕獲的excep 'NSInvalidArgumentException',原因:'不受支持的NSSortDescriptor選擇器:sizeCompare:'

這似乎與Harrington的陳述一致。另外,當我說我投到NSNumber時,我試圖簡短一些,但顯然還不清楚。這是我做過什麼:

NSNumberFormatter *numformatter = [[NSNumberFormatter alloc] init]; 
numformatter.numberStyle = NSNumberFormatterNoStyle; 
self.objectToEdit.size = [numformatter numberFromString:self.sizeTextField.text]; 

所以,我想數字排序一個NSString(1,2,11,25),而不是(1,11,2,25)。我仍然會欣賞任何解決方案。

+0

你的問題有點難以理解。有一次你說你有NSString,比你說你有核心數據。另外,你的「總結」說了另一件事。請優化您的問題並提供一些代碼片段。這將大大增加得到答案的機會。 – Krizz 2011-12-30 15:09:53

+0

NSNumber不是int。 – 2011-12-30 17:58:40

+0

@Krizz你說得對。這是一個糟糕的帖子。我試圖澄清。 – Kurt 2011-12-30 22:35:16

回答

8

我終於找到了數字排序字符串的問題的答案。它涉及到直接使用塊。對於我的排序描述符,它很簡單:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"size" ascending:NO comparator:^(id obj1, id obj2) { return [obj1 compare:obj2 options:NSNumericSearch]; }]; 

這允許NSStrings數值排序(1,11,2,25)。

我不知道爲什麼一個類別沒有工作。以爲我會爲遇到同樣問題的其他人發佈答案。

+0

這很可愛。謝謝。 (雖然塊來自魔鬼。) – CBGraham 2012-11-01 17:53:52

0

apparently only compare:, caseInsensitiveCompare:, localizedCompare: and localizedCaseInsensitiveCompare: work as sort selectors when doing a fetch in iOS

真的嗎?我認爲問題更多的是你使用的選擇器只能使用一個參數(另一個字符串進行比較),你需要使用-compare:options:做一個數字比較。

嘗試編寫一個類別方法來進行比較,例如

@interface NSString(NumericCompare) 

-(NSComparisionResult) compareNumerically: (NSString*) anotherString; 

@end 

@implementation NSString(NumericCompare) 

-(NSComparisionResult) compareNumerically: (NSString*) anotherString 
{ 
    return [self compare: anotherString options: NSNumericSearch]; 
} 

I have tried casting the string to an NSNumber. There is an error with ARC, as NSNumber is basically an int.

你不能只投一個NSSstring到一個NSNumber,你只鑄造指針,而不是指向的對象。你可以這樣做

NSNumber* number = [NSDecimalNumber numberWithString: myStringValue]; 

NSDecimalNumberNSNumber一個子類。

+0

謝謝你JeremyP。我感謝你的考慮。正如你所看到的,我嘗試了與你的幾乎相同的分類方法。您可以在我編輯的帖子中看到錯誤消息。 – Kurt 2011-12-30 22:37:39

0

如果確實有限制(實際上,我在我的代碼中只使用localizedCaseInsensitiveCompare:),則應該可以使用sortDescriptorWithKey:ascending:comparator:並在塊中使用NSNumericSearch。

0

我能夠在沒有NSNumericSearch的情況下完成我所需要的工作。

我改變核心數據結構,改變「尺寸」屬性以INTEGER16和改變從NSString的模型類屬性「尺寸」到的NSNumber,並做了輕質遷移。

然後我修改了現有的代碼只是用了一點:

aString = [aNumber stringValue]; 

NSNumberFormatter *numformatter = [[NSNumberFormatter alloc] init]; 
numformatter.numberStyle = NSNumberFormatterNoStyle; 
bNumber = [numformatter numberFromString:bString] 

酌情改變的NSString與NSNumber的對象。

現在它有點像我的願望。

總之,我通過改變數據結構避免數值排序NSString的的整個問題。

排序描述符只是在fetchedResultsController使用的基本要點:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey1 ascending:YES]; 
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"size" ascending:YES]; 
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor,sortDescriptor2, nil]; 

感謝大家的輸入。抱歉,添麻煩了。