嗨,我都试过一些解决办法,但没有运气。
我正在从数据核心获取文本,但是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)")
}
}
最佳答案
尝试使用if-let语句:
if let result = trans.value(forKey: "page22") {
page22TextView?.text = result
}
或尝试使用guard语句:
guard let result = trans.value(forKey: "page22") else { return }
page22TextView?.text = String(describing: result)
或者你可以强迫它上浮,就像:
let result = trans.value(forKey: "page22")
if result != nil {
page22TextView?.text = result! as! String
}
或者你可以按照下面@MrugeshTank建议的方式回答
关于swift - 在打印时显示的文本 View 中为 optional ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41992501/