【问题标题】:nil coalescing inside dictionary index using swift使用swift在字典索引内合并零
【发布时间】:2020-02-26 12:32:57
【问题描述】:

我正在尝试使用来自文本字段 textFieldOne.text!textFieldTwo.text! 的可选值为字典键分配一个值,但 Xcode 抛出构建错误。

let variable = [
        "keyOne": textFieldOne.text ?? "",
        "keyTwo": textFieldTwo.text ?? ""
] as [String : Any]

Build Failed as 编译器无法在合理的时间内对该表达式进行类型检查;尝试将表达式分解为不同的子表达式

【问题讨论】:

    标签: ios swift uitextfield nsdictionary coalescing


    【解决方案1】:

    您不必将text 属性默认为“”

    let variable = [
        "keyOne": textFieldOne.text!,
        "keyTwo": textFieldTwo.text!
     ] 
    

    查看https://developer.apple.com/documentation/uikit/uitextfield/1619635-text

    【讨论】:

    • 谢谢!如果它有一个初始化为 var variableName : String! 和内部字典 "keyThree": variableName 的变量怎么办。我可以使用"keyThree": variableName ?? ""
    • @DoubtAN 为什么不var variableName = ""
    【解决方案2】:

    你可以创建Dictionarylike,

    var variable = [String:Any]()
    variable["keyOne"] = textFieldOne.text
    variable["keyTwo"] = textFieldTwo.text
    

    【讨论】:

      猜你喜欢
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      • 2022-10-26
      • 2018-12-10
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多