UITextField - 无法解开 Optional.None [英] UITextField - Can't unwrap Optional.None

查看:25
本文介绍了UITextField - 无法解开 Optional.None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 Swift 进行编码,并尝试构建一个计算器.当我启动模拟器并单击计算器的按钮1"时,它会调用 Tap0 函数.我遇到了以下问题:Can't unwrap Optional.None.

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)
  }
}

我该如何解决?

推荐答案

按照 rdelmar 的建议,为了解决错误,将显示连接到 StoryBoard 中的 UITextField:

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

这篇关于UITextField - 无法解开 Optional.None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆