Swift CoreData FetchRequest - 如何通过使用另一个值从一个实体中提取一个值。

问题描述 投票:1回答:1

在我的CoreData设置中,我有一个实体,使用两个字符串。"name "和 "stadium"。

我想写一个函数,把 "name "作为参数,做一个CoreData的获取,然后返回 "name "实体对应的 "stadium "字符串值。这似乎不难,但我不知道从哪里开始......

谅谅

swift
1个回答
0
投票

你应该使用 NSPredicate 用于过滤。

let fetchRequest = NSFetchRequest<Team>(entityName: "Team")
fetchRequest.predicate = NSPredicate(format: "name == %@", nameToFind)
fetchRequest.fetchLimit = 1
let team = (try? context.fetch(fetchRequest))?.first

return team?.stadium
© www.soinside.com 2019 - 2024. All rights reserved.