NSOrderedSet 和 SwiftUI ForEach
NSOrderedSet and SwiftUI ForEach
我正在尝试使用 CoreData 和 SwiftUI 并有两个实体;狗和步行。
Dog
之间是一对多的关系
public class Dog: NSManagedObject, Identifiable {
@NSManaged public var name: String?
@NSManaged public var walks: NSOrderedSet?
}
步行
public class Walk: NSManagedObject, Identifiable {
@NSManaged public var date: Date?
@NSManaged public var dog: Dog?
}
我遇到的问题是在列表中显示选定狗的所有散步。下面的WalksView会显示当前所有的步行
struct WalksView: View {
@ObservedObject var dogWalksVM:DogWalkVM
var selectedIndex:Int
var body: some View {
let currentDog = dogWalksVM.dogs[selectedIndex]
return List {
if currentDog.walks == nil {
EmptyView()
} else {
ForEach(0..<currentDog.walks!.count) { index in
Text("Date \(self.walkDate(currentDog.walks![index] as! Walk))")
}
}
}
.navigationBarTitle(Text("Walks for \(dogWalksVM.dogs[selectedIndex].name ?? "")"), displayMode: .inline)
.navigationBarItems(trailing: Button(action: {
self.addWalk(for: currentDog)
}, label: {
Image(systemName: "plus")
}))
}
func walkDate(_ walk:Walk) -> String{
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
guard let walkDate = walk.date as Date? else { return ""}
return dateFormatter.string(from: walkDate)
}
func addWalk(for currentDog:Dog) {
self.dogWalksVM.addWalk(for: currentDog)
}
}
但是当我添加一个新的时,应用程序崩溃并出现以下错误:
ForEach<Range<Int>, Int, Text> count (1) != its initial count (0).
`ForEach(_:content:)` should only be used for *constant* data.
Instead conform data to `Identifiable` or use `ForEach(_:id:content:)` and provide an explicit `id`!
如果我尝试像这样在步行中执行 ForEach:
ForEach(Array(currentDog.walks!),id: \.self) { walk in
Text("Date \(self.walkDate(walk))")
}
有人告诉我
Protocol type 'NSOrderedSet.Element' (aka 'Any') cannot conform to 'Hashable' because only concrete types can conform to protocols
以下应该对您有所帮助
ForEach(Array(currentDog.walks!.set),id: \.self) { walk in
Text("Date \(self.walkDate(walk))")
}
如本 中所述,有一种更好的方法来迭代 NSOrderedSet。 Array(my_nsorderedset.set) 不会保留顺序!
相反,使用 mynsorderedset.array 作为! [包含类型].
我正在尝试使用 CoreData 和 SwiftUI 并有两个实体;狗和步行。 Dog
之间是一对多的关系public class Dog: NSManagedObject, Identifiable {
@NSManaged public var name: String?
@NSManaged public var walks: NSOrderedSet?
}
步行
public class Walk: NSManagedObject, Identifiable {
@NSManaged public var date: Date?
@NSManaged public var dog: Dog?
}
我遇到的问题是在列表中显示选定狗的所有散步。下面的WalksView会显示当前所有的步行
struct WalksView: View {
@ObservedObject var dogWalksVM:DogWalkVM
var selectedIndex:Int
var body: some View {
let currentDog = dogWalksVM.dogs[selectedIndex]
return List {
if currentDog.walks == nil {
EmptyView()
} else {
ForEach(0..<currentDog.walks!.count) { index in
Text("Date \(self.walkDate(currentDog.walks![index] as! Walk))")
}
}
}
.navigationBarTitle(Text("Walks for \(dogWalksVM.dogs[selectedIndex].name ?? "")"), displayMode: .inline)
.navigationBarItems(trailing: Button(action: {
self.addWalk(for: currentDog)
}, label: {
Image(systemName: "plus")
}))
}
func walkDate(_ walk:Walk) -> String{
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
guard let walkDate = walk.date as Date? else { return ""}
return dateFormatter.string(from: walkDate)
}
func addWalk(for currentDog:Dog) {
self.dogWalksVM.addWalk(for: currentDog)
}
}
但是当我添加一个新的时,应用程序崩溃并出现以下错误:
ForEach<Range<Int>, Int, Text> count (1) != its initial count (0).
`ForEach(_:content:)` should only be used for *constant* data.
Instead conform data to `Identifiable` or use `ForEach(_:id:content:)` and provide an explicit `id`!
如果我尝试像这样在步行中执行 ForEach:
ForEach(Array(currentDog.walks!),id: \.self) { walk in
Text("Date \(self.walkDate(walk))")
}
有人告诉我
Protocol type 'NSOrderedSet.Element' (aka 'Any') cannot conform to 'Hashable' because only concrete types can conform to protocols
以下应该对您有所帮助
ForEach(Array(currentDog.walks!.set),id: \.self) { walk in
Text("Date \(self.walkDate(walk))")
}
如本
相反,使用 mynsorderedset.array 作为! [包含类型].