0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

特定の条件に基づいて、Core Dataからオブジェクトを取得する方法

Posted at

特定の条件に基づいて、Core Dataからオブジェクトを取得する場合は、NSPredicateを使用する必要があります。NSPredicateは、オブジェクトのフィルタリングや検索に使用されます。

以下は、@FetchRequestにNSPredicateを追加し、特定のRecordingオブジェクトのみを取得する例です。

@FetchRequest(
    sortDescriptors: [NSSortDescriptor(keyPath: \Recording.created_at, ascending: true)],
    animation: .default,
    predicate: NSPredicate(format: "name == %@", "My Recording")
) var recordings: FetchedResults<Recording>


この例では、Recordingオブジェクトのname属性が"My Recording"であるオブジェクトのみを取得するために、NSPredicateを使用しています。NSPredicateでは、検索条件を指定するために、文字列形式のフォーマットを使用しています。formatパラメータに指定された文字列は、Recordingオブジェクトの属性名と比較する値を指定するフォーマットで、%@は文字列、%dは整数を表します。

このように、NSPredicateを使用することで、Core Dataから特定のRecordingオブジェクトのみを取得することができます

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?