swift - 在打印时显示的 TextView 中可选

标签 swift text option-type unwrap

大家好,我已经尝试了一些解决方案,但没有成功。

我正在从 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/

相关文章:

ios - 如何在编辑时更改 uitextfield leftView image tintColor

ios - UIApplication 上名为 -statusBar 或 -statusBarWindow 的应用程序

ios - AWS SDK iOS - 如何确定上传请求的完成百分比

javascript - 如何成功实现用于生成句子的下一个单词的马尔可夫模型?

css - 段落自动换行

html - 正则表达式匹配逗号之前但不之后的空格

具有可为空选项字段的 Rust 结构

ios - 在运行时更改 tableview 高度

ios - 标签打印可选()即使强制展开

haskell - 如何使 Maybe 的这个替代定义起作用?