私は自分で回避策を見つけましたが、それでも問題を理解しようとしています。
テキストフィールドが編集されるまで非表示になるuitableviewを使用して、オートコンプリートテキストフィールドを作成しました。UI部分は正常に機能します。問題となるのは結果部分の検索です。結果をキーの値で並べ替えたいので、結果を保存するためにローカルのNSMutableDictionaryを宣言しました。
辞書でkeysSortedByValueUsingSelectorを直接呼び出すと、クラッシュします。ただし、最初に[dict allKeys]でキーを取得してから、sortedArrayUsingSelectorを呼び出すと、正常に機能します。
// This commented out line will crash
// NSArray *sortedKeysArray = [dict keysSortedByValueUsingSelector:@selector(compare:)];
// The next two lines runs fine.
NSArray *keyArray = [dict allKeys];
NSArray *sortedKeysArray = [keyArray sortedArrayUsingSelector:@selector(compare:)];
検索メソッドの完全なソースコードは次のとおりです。
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{
// Put anything that starts with this substring into the autocompleteUrls array
// The items in this array is what will show up in the table view
[autocomplete_symbol_array removeAllObjects];
rRSIAppDelegate *appDelegate = (rRSIAppDelegate *)([[UIApplication sharedApplication] delegate]);
NSString *input_str = [substring uppercaseString];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
int i = 0;
for(SymbolInfo *symbol_info in appDelegate.m_symbol_info_array)
{
i++;
NSString *info_str = [[[symbol_info.m_symbol uppercaseString] stringByAppendingString:@"|"] stringByAppendingString:[symbol_info.m_company_name uppercaseString]];
NSUInteger pos = [info_str rangeOfString:input_str].location;
if (pos != NSNotFound)
{
int tmp = pos * 10000 + i;
NSNumber *map_key = [[NSNumber alloc] initWithInt:tmp];
[dict setObject:symbol_info forKey:map_key];
}
}
// This commented out line will crash
// NSArray *sortedKeysArray = [dict keysSortedByValueUsingSelector:@selector(compare:)];
// The next two lines runs fine.
NSArray *keyArray = [dict allKeys];
NSArray *sortedKeysArray = [keyArray sortedArrayUsingSelector:@selector(compare:)];
for (NSNumber *key in sortedKeysArray)
{
SymbolInfo *symbol_info = [dict objectForKey:key];
[autocomplete_symbol_array addObject:symbol_info];
}
// NSLog(@"everything added: %d", [autocomplete_symbol_array count]);
[autocompleteTableView reloadData];
}