【问题标题】:Swift Programming快速编程
【发布时间】:2014-10-16 07:16:41
【问题描述】:

我是 swift 语言的初学者。我收到此错误:

Error: Any object does not have member named subscript.

谁能给出解决方案?

我的代码:

        controller! = UIAlertController(title: "Welcome", message: "Hi...", preferredStyle: .Alert)

        let action = UIAlertAction(title: "Please enter atleat 10 characters", style: UIAlertActionStyle.Default, handler: {[weak self] (paramAction:UIAlertAction!) in

            let userName = self!.controller!.textFields[0].text
            println("your username is \(userName)")
        })

        controller!.addTextFieldWithConfigurationHandler({(textField:UITextField!) in
            textField.placeholder = "XXXXX-XXXXX"
        })

【问题讨论】:

  • 你是如何声明变量textFields的?
  • 实际上该代码在食谱中。我刚打了。我得到了那个错误..你能给出一个正确的代码吗??

标签: objective-c ios7 swift


【解决方案1】:

使用这个

var controller = UIAlertController(title: "Welcome", message: "Hi...", preferredStyle: UIAlertControllerStyle.Alert)

        let action = UIAlertAction(title: "Please enter atleat 10 characters", style: UIAlertActionStyle.Default, handler: {[weak self] (paramAction:UIAlertAction!) in

            var userName = controller.textFields?
            println("your username is \(userName)")
        })
        controller.addTextFieldWithConfigurationHandler({(textField:UITextField!) in
            textField.placeholder = "XXXXX-XXXXX"
        })
        controller.view.center = self.view.center
        self.view .addSubview(controller.view)

【讨论】:

    【解决方案2】:
    let action = UIAlertAction(title: "Next", style: UIAlertActionStyle.Default, handler: {[weak self](paramAction:UIAlertAction!) in
                    if let textFields = self!.controller?.textFields{
                    let theTextFields = textFields as [UITextField]
                    let userName = theTextFields[0].text
                    println("Your username is \(userName)")
                }
    
            })
    

    【讨论】:

      猜你喜欢
      • 2016-01-12
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多