【问题标题】:list of indexes in NSArray from NSPredicate来自 NSPredicate 的 NSArray 中的索引列表
【发布时间】:2015-08-31 11:46:08
【问题描述】:

我的问题非常类似于:obj c -get list of indexes in NSArray from NSPredicate

但是有区别。我有两个 NSArray。第一个是姓名,另一个是电话号码。我使用以下代码搜索“名称”数组以搜索名称:

-(void)filterContentForSearchText:(NSString *)searchtext scope:(NSString *)scope {

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",searchtext];
self.searchResult = [ForMsgNamrarray filteredArrayUsingPredicate:predicate];

}

我返回了一个过滤后的数组,这很好。但根据过滤数组的索引,我还想过滤包含电话号码的数组。

例如,如果我搜索“jh”,我会在搜索数组中找到“Jhone”。我还想在另一个数组中添加约翰的电话号码,即“searchArrayPhNum”

【问题讨论】:

  • 做一件简单的事。为每个包含名称和电话号码作为键的记录准备一个 dic,并将它们添加到数组中。并根据需要进行过滤..
  • 我不认为使用两个“像这样链接”的 NSArray 是一个好的架构...

标签: ios objective-c nsarray nspredicate


【解决方案1】:

准备一个具有以下属性的模型对象,

@interface Person : NSObject

@property (nonatomic,retain) NSString *name;
@property (nonatomic,retain) NSString *phoneNumber;
@end

并使用单个数组(Person 对象的数组)而不是两个不同的数组, 你的谓词会改变,

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name beginswith[c] %@",searchtext]; 

【讨论】:

    猜你喜欢
    • 2013-12-19
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多