类’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,取消需要显式给它一个值或初始化它。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- WinCE下将SQLite数据库导入到另外一个SQLite数据库
- actionscript-3 – JWPlayer:尝试将视频播放器绑定在我自己
- OCP-043 V$FLASHBACK_DATABSE_LOG & V$FLASHBACK_DATAB
- 如何用Ruby中的哈希收集值替换一个字符串?
- c# – WebResponse.GetResponseStream()可以返回null吗?
- 比较两个ip与C#
- SQLite多线程使用总结
- 使用Cocos2d-lua开发植物大战僵尸05-植物卡片类的实现
- XML与JSON的区别
- Plugin execution not covered by lifecycle configuration