コアデータ - 時間帯NSPredicateフォーマット(NSTimeInterval)

StackOverflow https://stackoverflow.com/questions/1099009

  •  11-09-2019
  •  | 
  •  

質問

私はCoreData情報を通過し、NSTimeInterval内にあるcreatedAt(NSDateとしての私のオブジェクトの一部)を持つオブジェクトを検索する方法を見つけようとしています。私はこれを設定する方法を教えてください。

私はでドキュメント上で見てきました: http://developer.apple.com/documentation/Cocoa/Conceptual/述語/ predicates.htmlする

しかし、私はそこに何かを見つけていないよ。

私は2つのタイムスタンプを作成し、BETWEEN SQLのを使用する必要がありますか?

すべてのヘルプは素晴らしいだろう。

役に立ちましたか?

解決

すべての

まず、それはNSTimeIntervalはちょうど時間の長さではなく、その場所を指定しているためNSDateは、NSTimeInterval内にあるかどうかを確認しても意味がありません。代わりに、あなたはあなたの間隔の開始と終了を指定する二つの別々のNSDatesを使用したい。

ここでは、(beginningTimeとendTimeのはNSDatesある)次のようになります。

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"YourEntityName" inManagedObjectContext:yourContext];
NSPredicate *beginningPredicate = [NSPredicate predicateWithFormat:@"createdAt >= %@", beginningTime];
NSPredicate *endPredicate = [NSPredicate predicateWithFormat:@"createdAt <= %@", endTime];
request.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:beginningPredicate, endPredicate, nil]];
NSArray *results = [yourContext executeFetchRequest:request error:NULL];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top