gpt4 book ai didi

SwiftUI - 从核心数据 NSSet 中删除 - @FetchRequest with .onDelete()

转载 作者:行者123 更新时间:2023-12-05 06:19:01 26 4
gpt4 key购买 nike

我正在学习免费和公开的 Hacking With Swift 类(class)。不幸的是,关于 Core Data 的章节没有解释如何删除 NSSet 中用于一对多关系的对象。我查看了文档并尝试弄清楚如何使用生成的访问器,但收效甚微。如果有人能够解释如何做到这一点,我将不胜感激,因为我最想知道!!
One To Many Relationship

到目前为止我尝试过但失败了!!!


 func deleteCandy (at offsets: IndexSet) {
for offset in offsets {
let candy = countries[offset].candyArray[offset]
moc.delete(candy)
if self.moc.hasChanges{
try? moc.save()
}
}
}

类:

扩展国家{

@nonobjc public class func fetchRequest() -> NSFetchRequest<Country> {
return NSFetchRequest<Country>(entityName: "Country")
}

@NSManaged public var fullName: String?
public var wrappedFullName: String {
fullName ?? "Unknown Full Name"
}
@NSManaged public var shortName: String?
public var wrappedShortName: String{
shortName ?? "Unknown Short Name"

}
public var candyArray:[Candy] {
let set = candy as? Set<Candy> ?? []
return set.sorted {
$0.wrappedName < $1.wrappedName
}
}


@NSManaged public var candy: NSSet?

//MARK:为糖果生成访问器扩展国家 {

@objc(addCandyObject:)
@NSManaged public func addToCandy(_ value: Candy)

@objc(removeCandyObject:)
@NSManaged public func removeFromCandy(_ value: Candy)

@objc(addCandy:)
@NSManaged public func addToCandy(_ values: NSSet)

@objc(removeCandy:)
@NSManaged public func removeFromCandy(_ values: NSSet)

内容 View @Environment(.managedObjectContext) var moc @FetchRequest(entity: Country.entity(), sortDescriptors: []) var countries: FetchedResults

var body: some View {

VStack {
List {
ForEach(countries, id: \.self) { country in
Section(header: Text(country.wrappedFullName)) {
ForEach(country.candyArray, id: \.self) { candy in
Text(candy.wrappedName)
}
}
}.onDelete(perform: deleteCandy)
}

Button("Add") {
let candy1 = Candy(context: self.moc)
candy1.name = "Mars"
candy1.origin = Country(context: self.moc)
candy1.origin?.shortName = "UK"
candy1.origin?.fullName = "United Kingdom"

let candy2 = Candy(context: self.moc)
candy2.name = "KitKat"
candy2.origin = Country(context: self.moc)
candy2.origin?.shortName = "UK"
candy2.origin?.fullName = "United Kingdom"

let candy3 = Candy(context: self.moc)
candy3.name = "Twix"
candy3.origin = Country(context: self.moc)
candy3.origin?.shortName = "UK"
candy3.origin?.fullName = "United Kingdom"

let candy4 = Candy(context: self.moc)
candy4.name = "Toblerone"
candy4.origin = Country(context: self.moc)
candy4.origin?.shortName = "CH"
candy4.origin?.fullName = "Switzerland"

try? self.moc.save()
}
}

}

func deleteCandy (at offsets: IndexSet) {
for offset in offsets {
let candy = countries[offset].candyArray[offset]
moc.delete(candy)
if self.moc.hasChanges{
try? moc.save()
}
}
}

} The UI

最佳答案

最后,这个从 DMG 传递闭包的解决方案非常有效:

Well explained solution here.

func deleteCandies(at offsets: IndexSet, from country: Country) {
for offset in offsets.sorted().reversed() {
let candyToDelete = country.candyArray[offset]
country.removeFromCandy(candyToDelete)
moc.delete(candyToDelete)
}
if moc.hasChanges{
try? moc.save()
}
}



VStack {
List {
ForEach(countries, id: \.self) { country in
Section(header: Text(country.wrappedFullName)) {
ForEach(country.candyArray, id: \.self) { candy in
Text(candy.wrappedName)
}
.onDelete(perform: { offsets in
self.deleteCandies(at: offsets, from: country)
})
}
}
}

关于SwiftUI - 从核心数据 NSSet 中删除 - @FetchRequest with .onDelete(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61026300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com