Swift 5 Nsfetchrequest谓词尝试查找字符串UUID

发布于 2025-02-03 17:09:26 字数 775 浏览 2 评论 0原文

我有一个字符串的UUID进入此方法,以查找Coredata中的一个实体,该实体将UUID保存为UUID类型(不是字符串)。

我不断遇到“致命错误:在谓词上拆开可选值时出乎意料地发现了无效”。

func loadUser(uuid: String) -> [ExistingUsers2] {
    let request : NSFetchRequest<ExistingUsers2> = ExistingUsers2.fetchRequest()
    let uuidQuery = NSUUID(uuidString: uuid)
    request.predicate = NSPredicate(format: "%K == %@", #keyPath(ExistingUsers2.uuid), uuidQuery! as CVarArg)
    request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
    do {
        existingUsersArray = try context.fetch(request)
        print("Returned \(existingUsersArray.count)")
    } catch {
        print("Error fetching data from context \(error)")
    }
    return existingUsersArray
}

有帮助吗?我在这里什么都没找到或Google博士。 TKS

I have a string UUID coming into this method, to lookup an entity in CoreData that has UUID's saved as UUID type (Not String).

I keep getting "Fatal error: Unexpectedly found nil while unwrapping an Optional value" on line for the predicate.

func loadUser(uuid: String) -> [ExistingUsers2] {
    let request : NSFetchRequest<ExistingUsers2> = ExistingUsers2.fetchRequest()
    let uuidQuery = NSUUID(uuidString: uuid)
    request.predicate = NSPredicate(format: "%K == %@", #keyPath(ExistingUsers2.uuid), uuidQuery! as CVarArg)
    request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
    do {
        existingUsersArray = try context.fetch(request)
        print("Returned \(existingUsersArray.count)")
    } catch {
        print("Error fetching data from context \(error)")
    }
    return existingUsersArray
}

Any help? I haven't found anything here or Dr Google. TKS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小镇女孩 2025-02-10 17:09:26

您可以用以下方式替换谓词:

guard let uuidQuery = UUID(uuidString: uuid) else { return [] } // no valid UUID with this code
request.predicate = NSPredicate(format: "%K == %@", #keyPath(ExistingUsers2.uuid), uuidQuery as CVarArg)

其他所有内容都应起作用。

更新

这是最终有效的代码,感谢您的帮助 @andréHenriqueda Silva

func loadUser(uuid: String) -> [ExistingUsers2] {
    let request : NSFetchRequest<ExistingUsers2> = ExistingUsers2.fetchRequest()
    let uuidQuery = NSUUID(uuidString: uuid)
    request.predicate = NSPredicate(format: "uuid == %@", uuidQuery! as CVarArg)
    request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
    do {
        existingUsersArray = try context.fetch(request)
    } catch {
        print("Error fetching data from context \(error)")
    }
    return existingUsersArray
}

You can replace your predicate with this:

guard let uuidQuery = UUID(uuidString: uuid) else { return [] } // no valid UUID with this code
request.predicate = NSPredicate(format: "%K == %@", #keyPath(ExistingUsers2.uuid), uuidQuery as CVarArg)

Everything else should work.

UPDATE

This is the code that finally worked, thanks for your help @André Henrique da Silva

func loadUser(uuid: String) -> [ExistingUsers2] {
    let request : NSFetchRequest<ExistingUsers2> = ExistingUsers2.fetchRequest()
    let uuidQuery = NSUUID(uuidString: uuid)
    request.predicate = NSPredicate(format: "uuid == %@", uuidQuery! as CVarArg)
    request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
    do {
        existingUsersArray = try context.fetch(request)
    } catch {
        print("Error fetching data from context \(error)")
    }
    return existingUsersArray
}
千秋岁 2025-02-10 17:09:26

尝试以您的谓词:nspredicate(格式:“ cid =%@”,“ \(id)”)

其中ciduuid中,CoredataID是您从字符串中获得的UUID。也不使用nsuuid

Try this as your predicate: NSPredicate(format: "cid = %@", "\(id)")

where cid is the UUID in CoreData and id is the UUID you got from the string. Also do not use NSUUID.

独木成林 2025-02-10 17:09:26

用您的属性名称替换uuidattributename yourstringuuid ,用要转换为UUID类型的字符串。

var uuid = UUID(uuidString: yourStringuuid)

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ExistingUsers2")
fetchRequest.predicate = NSPredicate(format: "uuidAttributeName == %@",uuid)

let results = try context.fetch(fetchRequest)

Replace the uuidAttributeName with your attribute name and yourStringuuid with the your string that you want to convert into UUID type.

var uuid = UUID(uuidString: yourStringuuid)

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ExistingUsers2")
fetchRequest.predicate = NSPredicate(format: "uuidAttributeName == %@",uuid)

let results = try context.fetch(fetchRequest)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文