加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

类’ViewController’在swift中没有初始化

发布时间:2020-12-14 06:13:47 所属栏目:百科 来源:网络整理
导读:当我这样做时,从编译器获取投诉 class ViewController: UIViewController { var delegate : AppDelegate override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. //self.appDelegate
当我这样做时,从编译器获取投诉
class ViewController: UIViewController {

    var delegate : AppDelegate
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
        //self.appDelegate = UIApplication.sharedApplication().delegate;

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func getData(sender : AnyObject) {

    }

    @IBAction func LogOut(sender : AnyObject) {
    }
}

但是,如果我只是添加?在AppDelegate的结尾,如下所示,错误消失了。

class ViewController: UIViewController {

    var delegate : AppDelegate?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
        //self.appDelegate = UIApplication.sharedApplication().delegate;

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func getData(sender : AnyObject) {

    }

    @IBAction func LogOut(sender : AnyObject) {
    }
}

我没有看到可选关键字与此错误相关,除非我错了。
有什么想法。随意评论。欣赏它。

谢谢

错误可以改进,但是你的第一个版本的问题是你有一个成员变量,委托,没有默认值。 Swift中的所有变量必须始终具有值。这意味着你必须在一个初始化器中设置它,你没有,或者你可以提供一个默认值在线。

当你使它可选时,默认情况下允许它为nil,取消需要显式给它一个值或初始化它。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读