SwiftUI - Core-Data - 在这种特殊情况下访问包装的值
Posted
技术标签:
【中文标题】SwiftUI - Core-Data - 在这种特殊情况下访问包装的值【英文标题】:SwiftUI - Core-Data - Access to wrapped value in this kind of special case 【发布时间】:2020-09-07 17:02:35 【问题描述】:使用下面的代码(见下面的代码),我得到了这种结果(见下面的结果): 从下面显示的代码开始(不使用视图...),我怎样才能访问 WrappedValues 而不是这种结果?
感谢您的帮助!
结果:
<Translation: 0x600002132440> (entity: Translation; id: 0x6742c7fbe17c81d1 <x-coredata://0B558EF2-1CC5-4649-9D70-560DAA900FFE/Translation/p21>; data:
id = 11;
language = English;
wording = "Select the Folders to Rename...";
)
代码:
class FetchWording: ObservableObject
func fetch (pID: String) -> String
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Translation")
fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [NSPredicate(format: "id == %@", pID as CVarArg), NSPredicate(format: "language == %@", sDisplayLanguage as CVarArg)])
fetchRequest.fetchLimit = 1
do
let fetchWording = try ContentView().managedObjectContext.fetch(fetchRequest)
print (fetchWording[0])
catch
print ("Error")
return (“See you later when it’s working”)
【问题讨论】:
【参考方案1】:…随便找这个,不是很满意:
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Translation")
fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [NSPredicate(format: "id == %@", pID as CVarArg), NSPredicate(format: "language == %@", sDisplayLanguage as CVarArg)])
fetchRequest.fetchLimit = 1
var fetchWording = [NSManagedObject]()
do
let records = try ContentView().managedObjectContext.fetch(fetchRequest)
if let records = records as? [NSManagedObject]
fetchWording = records
return **fetchWording.first!.value(forKey: "wording")! as! String**
catch
print ("Unexpected error: Function fetch: Wording not found!")
【讨论】:
以上是关于SwiftUI - Core-Data - 在这种特殊情况下访问包装的值的主要内容,如果未能解决你的问题,请参考以下文章
在单个 NSTableView (Core-Data) 中显示来自多个实体的数据