【问题标题】:Value of type 'Any' has no member 'text in swift 4'Any'类型的值在swift 4中没有成员'文本
【发布时间】:2018-03-18 05:49:36
【问题描述】:
@IBAction func endEditingText(_ sender: Any) {

    let baseURL = "http://api.openweathermap.org/data/2.5/forecast?q"
    let APIKeysString = "&appid=14b92e1046c4b6e4f4d5adda8259131b"
    guard let cityString = sender.text else {return}
    if let finalURL = URL (string:baseURL + cityString + APIKeysString) {
        requestWeatherDate(url: finalURL)

    } else {
        print("error")
    }
}

【问题讨论】:

    标签: ios swift4


    【解决方案1】:

    您可以键入 cast sender 到 UITextField。

    @IBAction func endEditingText(_ sender: Any) {
        let textFieldObject = sender as! UITextField
        ....
    }
    

    或者在创建 IBAction 时,您可以使用 UITextField 代替 Any。

    @IBAction func endEditingText(_ sender: UITextField) {
        ....
    }
    

    【讨论】:

    • 当 v 想要使用特定的东西,如 uitextfiled 或任何其他方法时 v 需要强制转换??
    【解决方案2】:

    错误很明显。 sender 被声明为 Any,因此编译器不知道实际的静态类型。

    改成UITextField:

    @IBAction func endEditingText(_ sender: UITextField) { ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 2018-11-07
      相关资源
      最近更新 更多