-1

NSArrayをフィルタリングしたいのですが。この配列には、名前、町、電話番号などの多くの情報が含まれています。しかし、いくつかの町は2〜3回配列されています。
町のある物件があります。
だから私はプロパティと一致するarryからのそれらのオブジェクトだけが欲しいです。

例:アレイスタンド:

  1. フランク、ニューヨーク、123456
  2. オリバー、ニューヨーク、123456
  3. トーマス、ボストン、123456

プロパティがニューヨークの場合、olnyオブジェクト1と2が必要です。

誰かが私がそれをどのように行うことができるか考えていますか?

これは私のコードです:

NSString *filterString = newsArticle;
NSPredicate *prediacte = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"Ort == '%@'",filterString]];
newsTownArray = [news filteredArrayUsingPredicate:predicate];

そして私がラインに来るとき:

cell.textLabel.text=[[newsTownArray objectAtIndex:indexPath.row] objectForKey:"Name"];
4

3 に答える 3

4

これにはNSPredicateを使用する必要があります。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"town == 'New York'"];
[yourArray filterUsingPredicate:predicate];

次のような述語を動的に作成できます。

NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"town == '%@'",yourInput]];

これyourInputNSString、必要な町名を保持する です。

詳細については、次の記事を確認してください。

  1. コードプロジェクト
  2. あなたのパンを使う
于 2013-01-21T12:58:41.910 に答える
0

ただし、NSPredicateを使用して配列内で実行できますが、少し異なる方法で実行することをお勧めします。これにより、コードと優れたプログラミング方法が追加されます。

Personこれらのプロパティを持つnameカスタムクラスを作成しcityますtelephone

のオブジェクトを格納する配列を作成しますPerson

この後、操作/フィルター/並べ替えなどを非常に簡単に行うことができます。


NSString *filterCity=@"Delhi";

NSMutableArray *yourArray=[NSMutableArray arrayWithArray:self.persons];
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"city == '%@'",filterCity]];
[yourArray filterUsingPredicate:predicate];

NSLog(@"Filtered");
for (Person *per in yourArray) {
    NSLog(@"Name: %@, City: %@, Telephone: %@",per.name, per.city, per.telephone);

}
于 2013-01-21T12:58:33.697 に答える
0

このコードを使用

NSMutableArray *subpredicates = [NSMutableArray array];

    for(NSString *term in arryOfWordsToBeSearched) {
        NSPredicate *p = [NSPredicate predicateWithFormat:@"self contains[cd] %@",term];
        [subpredicates addObject:p];
        }

     NSPredicate *filter = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
    result = (NSMutableArray*)[arryOfDummyData filteredArrayUsingPredicate: filter];
于 2013-01-21T13:03:02.497 に答える