core-data - 带有 Core Data 的 SwiftUI:获取带有谓词崩溃的请求
问题描述
我已成功将 Core Data 添加到我的 SwiftUI 项目中。我需要按类型过滤结果。当我向获取请求添加谓词时,应用程序在包含获取请求的视图尝试加载时崩溃。
错误是线程 1:EXC_BAD_ACCESS (code=1, address=0x1)
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: Task.entity(),
sortDescriptors:[
NSSortDescriptor(keyPath: \Task.type, ascending: true),
NSSortDescriptor(keyPath: \Task.totalScore, ascending: false),
],
predicate: NSPredicate(format: "type == %@", Int16(1))) //this is the line that crashes the app
var tasks: FetchedResults<Task>
如果我将 Int16(1) 更改为 Int16(0),应用程序不会崩溃,但列表中不会出现任何数据。
这是我使用核心数据编写的第一个应用程序,所以我需要帮助。
解决方案
感谢安德鲁建造者。他让我找到了正确的方向。使用需要 Int 的谓词的正确方法是:
predicate: NSPredicate(format: "type == %i", Int16(1)))
当我应该使用 %i 时,我使用了 %@。
推荐阅读
- android - 在 android studio 中找不到“转换为 MotionLayout”
- google-apps-script - CacheServices 用户缓存存储在 Google Apps 脚本中的什么位置?
- css - 在弹性框中重用具有绝对位置的组件
- java - 尝试返回数组中的单个对象时,如何在 for 循环中修复“缺少返回语句”?
- mongodb - 从接口获取密钥
- python - 使用模型参数时,IBM Watson Speech to Text in Python 给出 404
- vb.net - 从 SQL Server 搜索数据并显示到文本框和数据网格 VB.Net
- javascript - 在 Angular 中调用 AuthGuard 之前先加载数据
- python - Azure 函数使用 sendgrid 发送带有徽标的电子邮件
- javascript - 传单地图不会出现