UITextField - Can't unwrap Optional.None

天大地大妈咪最大 提交于 2019-12-25 07:34:59

问题


I just started coding with Swift and I try to build a calculator. When I launch the simulator and I click the button '1' of the calculator it calls Tap0 function. I encountered the following issue: Can't unwrap Optional.None.

import UIKit

class ViewController: UIViewController {
  @IBOutlet var display:UITextField

  override func viewDidLoad() {
    super.viewDidLoad()
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }

  @IBAction func Tap0(sender:AnyObject){AddNumber("0")}

  func AddNumber(x:String){
    self.display.text="" //ERROR (Can't unwrap Optional.None)
    println(self.display.text)
  }
}

How I can solve it?


回答1:


As what rdelmar suggested, to solve the error, connect display to the UITextField in StoryBoard:



来源:https://stackoverflow.com/questions/24562077/uitextfield-cant-unwrap-optional-none

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!