Swift String Interpolation displaying optional?

前端 未结 4 1542
感情败类
感情败类 2020-12-10 12:25

When i use the following code and have nameTextField be \"Jeffrey\" (or any other name)

@IBAction func helloWorldAction(nameTextField: UITextField) {

    na         


        
4条回答
  •  悲哀的现实
    2020-12-10 12:36

    Optionals must be unwrapped. You must check for it or force unwrap as you do. Imagine the optional as a box where you put a value. Before you can access it, you need to put it out.

    if let name = nameTextField.text {
        nameLabel.text = "Hello, \(name)"
    }
    

提交回复
热议问题