大家好,我已经尝试了一些解决方案,但没有成功。
我正在从 Data Core 获取文本,但 TextView 上有可选内容。
打印时在文本中显示可选。
page22TextView?.text = ("\(trans.value(forKey: "page22"))")
任何人都可以阐明这一点!已尝试打开包装,但它仍然低于:显示。
完整功能如下:
func getTranscriptions () {
//create a fetch request, telling it about the entity
let fetchRequest: NSFetchRequest<TextInputs> = TextInputs.fetchRequest()
do {
//go get the results
let searchResults = try getContext().fetch(fetchRequest)
//I like to check the size of the returned results!
print ("num of results = \(searchResults.count)")
//You need to convert to NSManagedObject to use 'for' loops
for trans in searchResults as [NSManagedObject] {
page22TextView?.text = ("\(trans.value(forKey: "page22"))")
//get the Key Value pairs (although there may be a better way to do that...
print("\(trans.value(forKey: "page22"))")
}
} catch {
print("Error with request: \(error)")
}
}
最佳答案
尝试设置获取nil值的默认值
page22TextView?.text = (trans.value(forKey: "page22") as? String) ?? ""
它将从 trans
设置您的值,如果它重新运行 nill
将由 ""
设置。
希望对你有帮助。
关于swift - 在打印时显示的 TextView 中可选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41992501/