iphone - 对 NSString 进行排序

标签 iphone objective-c ios

我有一个 iOS 应用程序,它有一个搜索栏和一个 UITableView。当我点击搜索栏(例如“ta”)时,网络服务会向我返回所有包含“at”的单词, 例如“beta”,“mota”,“at work”,“ebebebatbcbcb”,我只想包含以“at”开头的单词,而不是所有包含“at”的单词。

感谢您的回答。

最佳答案

试试这个:

-(NSMutableArray *)array:(NSMutableArray *)array withstart:(NSString *)string{


NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",string];
[array filterUsingPredicate:sPredicate];
return array;
}

或者其他方式:

NSString *prefix = @"at";     
NSArray *final_array=[array objectsAtIndexes:[array indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop)
                                              {
                                                  return [obj hasPrefix:prefix];
                                              }]];

关于iphone - 对 NSString 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9530494/

相关文章:

iphone - iOS - 在 ARC 下弹出时 ViewController 未被释放

objective-c - iOS - 不能使用 'super' 作为引用?

iphone - 一个 nib 文件显示在另一个文件上

ios - 如何在以前为 iPhone 构建的应用程序中为 iPad 添加 .xib 文件?

ios - 如何在 xcode 的单 View 应用程序中创建带有按钮的方向键

ios - 手动调用的 ViewController 被 "Show"Segue 覆盖

iphone - NavigationItem.BackButtonItem 为空...?

ios - 从 NSData 分配 AVAsset

ios - swift 3 : Swipe to 'Flag' , 如何

objective-c - 无法结束 BackgroundTask : no background task exists with identifier, 或者它可能已经结束