Core data 初始化带有核心数据的自定义绑定
到目前为止,我唯一使用CoreData的是列表,并使用fetch请求填充它们 那么,我该如何将一个Bool保存为true或false以供查看呢 bool将用于填充⭐️ 我的想法是在我的CoreData类上传递一个函数Core data 初始化带有核心数据的自定义绑定,core-data,swiftui,Core Data,Swiftui,到目前为止,我唯一使用CoreData的是列表,并使用fetch请求填充它们 那么,我该如何将一个Bool保存为true或false以供查看呢 bool将用于填充⭐️ 我的想法是在我的CoreData类上传递一个函数 static func isFavoritedCheck(using movieID: Int, context: NSManagedObjectContext) -> Bool { let model = MovieData(context: context)
static func isFavoritedCheck(using movieID: Int, context: NSManagedObjectContext) -> Bool {
let model = MovieData(context: context)
return movieID == model.movieID && model.favorite == true
}
到视图中的自定义绑定。。这是可能的还是使用FetchRequest是唯一的方法
struct StarView: View {
@Binding var favorited: Bool
let movieID: Int
let storage: StorageProvider
init(movieID: Int, storage: StorageProvider) {
self.movieID = movieID
self.storage = storage
_favorited = Binding //stuck here
}
是的,这是可能的,像这样,你需要把它包装成装订
struct StarView: View {
let movieID: Int
let storage: StorageProvider
@Binding var favorited: Bool
init(movieID: Int, storage: StorageProvider, favorited: Binding<Bool>) {
self.movieID = movieID
self.storage = storage
_favorited = favorited
}
var body: some View {
//. . .
}
}
结构视图:视图{
let movieID:Int
让存储:StorageProvider
@绑定变量:Bool
init(movieID:Int,storage:StorageProvider,favorited:Binding){
self.movieID=movieID
self.storage=存储
_偏爱的
}
变量体:某些视图{
//. . .
}
}
你回答了一半的问题,但我并没有故意把
favorited
放在参数列表中,因为我想从init主体中的CoreData属性中给它一个值