使用SwiftUI ForEach从NSOrderedSet获取字符串值 [英] Getting a String value from NSOrderedSet using SwiftUI ForEach
问题描述
使用此问题/答案我能够使用ForEach来使用从中创建的NSOrderedSet在CoreData中是一对多关系,但是我似乎无法访问存储在Core Data实体中的字符串属性。
Using this question/answer I am able to use ForEach to use a NSOrderedSet created from a One to Many relationship in CoreData, however I can't seem to access a String Attribute stored in the Core Data entity.
我有两个CoreData实体:Client& SessionNote。客户端可以有很多SessionNotes,clientNotes的NSOrderedSet,而SessionNote只能有一个Client。
I have two CoreData entities: Client & SessionNote. Clients can have many SessionNotes, the NSOrderedSet of clientNotes, and SessionNote can only have one Client.
Client + CoreDataClass.swift:
Client+CoreDataClass.swift:
public class Client: NSManagedObject {
}
Client + CoreDataProperties.swift:
Client+CoreDataProperties.swift:
extension Client {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Client> {
return NSFetchRequest<Client>(entityName: "Client")
}
@NSManaged public var firstName: String?
@NSManaged public var lastName: String?
@NSManaged public var id: UUID?
@NSManaged public var clientNotes: NSOrderedSet?
}
SessionNote + CoreDataClass.swift:
SessionNote+CoreDataClass.swift:
public class SessionNote: NSManagedObject {
}
SessionNote + CoreDataProperties.swift:
SessionNote+CoreDataProperties.swift:
extension SessionNote {
@nonobjc public class func fetchRequest() -> NSFetchRequest<SessionNote> {
return NSFetchRequest<SessionNote>(entityName: "SessionNote")
}
@NSManaged public var date: Date?
@NSManaged public var noteText: String?
@NSManaged public var id: UUID?
@NSManaged public var clientOrigin: Client?
}
这是ClientDetailView.swift:
and here is ClientDetailView.swift:
struct ClientDetailView: View {
@Environment(\.managedObjectContext) var moc
@ObservedObject var selectedClient: Client
var body: some View {
Form {
HStack {
Text("\(selectedClient.firstName ?? "") \(selectedClient.lastName ?? "")")
}
Section() {
ForEach(Array(selectedClient.clientNotes!.set), id: \.self) { note in
Text("This will repeat for the number of notes in the clientNotes NSOrderedSet")
/*
But instead I want to be able to display the noteText
string attribute stored in the SessionNote CoreData entity
instead of the above placeholder.
*/
}
}
}
}
}
我尝试过 Text( \(note.noteText ??))
但会引发以下错误:
I've tried Text("\(note.noteText ?? "")")
but it throws the following error:
类型 AnyHashable的值没有成员 noteText
Value of type 'AnyHashable' has no member 'noteText'
当我尝试 Text( \(self.selectedClient.clientNotes!.value(forKeyPath:\SessionNote.noteText)))
它将引发以下错误:
When I tried Text("\(self.selectedClient.clientNotes!.value(forKeyPath: \SessionNote.noteText))")
it throws the following error:
协议类型'Any'无法符合'_FormatSpecifiable',因为只有具体类型可以符合协议
Protocol type 'Any' cannot conform to '_FormatSpecifiable' because only concrete types can conform to protocols
是否仍要为每个SessionNote noteText实体显示不同的String值?
Is there anyway to display the different String values for each of the SessionNote noteText entity?
推荐答案
您可以在 NSOrderedSet $ c $上使用
array
方法c>并将其强制类型转换为 [SessionNote]
,因为您知道您将始终将 SessionNote
类存储到有序集合中。
You can use array
method on NSOrderedSet
and force typecast it to [SessionNote]
, since you know that you will always be storing SessionNote
class to the ordered set.
ForEach(selectedClient.clientNotes!.array as! [SessionNote], id: \.self) { (note: SessionNote) in
Text(note.text ?? "")
}
由于在您希望使用笔记的每个地方都必须进行此转换。您还可以在 Client
内添加一个计算属性,该属性始终为您提供 SessionNote
数组的键入版本。
Since this casting will be necessary in every places you wish to use the notes. You could also add a computed property inside your Client
which always gives you typed version of the SessionNote
array.
extension Client {
var typedClientNotes: [SessionNote] {
return (clientNotes?.array as? [SessionNote]) ?? []
}
}
而且,通过这些新更改,您可以进行您的 ForEach
会更好。
And, with these new changes you can make your ForEach
bit better.
ForEach(selectedClient.typedClientNotes, id: \.self) { note in
Text(note.text ?? "")
}
由于这个原因,我尝试将普通的 Set
用于Core数据关系。因为它可以被键入并使使用Coredata变得非常有趣。但是,尽管 NSOrderedSet
有一个无类型的集合,但它具有自己的优势。
For this reason, I try to use normal Set
for Core data relationship. Because it can be typed and makes working with Coredata so much fun. But, NSOrderedSet
has its own advantages despite of being an untyped collection.
这篇关于使用SwiftUI ForEach从NSOrderedSet获取字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!