您现在的位置是:首页 > 电脑 > 

声明计算变量后,编译器启动抱怨类没有初始化器(After declaring computed variable, compiler started complaining class has no initializer)

2025-07-18 21:59:16
声明计算变量后,编译器启动抱怨类没有初始化器(After declaring computed variable, compiler started complaining class has no initializer) 我创建了一个继承UITableViewCell的类: class MyCell: UITableViewCell { overri
声明计算变量后,编译器启动抱怨类没有初始化器(After declaring computed variable, compiler started complaining class has no initializer)

我创建了一个继承UITableViewCell的类:

class MyCell: UITableViewCell { override func awakeFromib() { super.awakeFromib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }

没有编译器错误。 然后,我声明了一个计算变量:

class MyCell: UITableViewCell { var student: Student{ willSet {...} } ... }

现在,编译器开始抱怨Class 'MyCell' has no initializers 。 然后,我定义了一个空的初始化:

class MyCell: UITableViewCell { init(){} var student: Student{ willSet {...} } ... }

错误仍然存​​在。 为什么在声明计算变量之后,编译器开始抱怨错误以及如何摆脱该错误?

I have created a class inherits UITableViewCell:

class MyCell: UITableViewCell { override func awakeFromib() { super.awakeFromib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }

There is no compiler error. Then, I declared a computed variable:

class MyCell: UITableViewCell { var student: Student{ willSet {...} } ... }

ow, compiler start complaining Class 'MyCell' has no initializers. Then, I defined an empty initialier:

class MyCell: UITableViewCell { init(){} var student: Student{ willSet {...} } ... }

The error still exist. Why after declared computed variable, compiler starts complaining that error & how to get rid of that error?

最满意答案

你应该试试这些:

选项1:

var student: Student! { willSet {...} }

选项2:

var student: Student? { willSet {...} }

因为我们必须初始化变量,否则编译器会给出错误。 在选项1中,我们将此变量声明为隐式解包可选 。 这意味着它的初始值为零,但它的价值将来不会为零。 在选项2中,我们将此变量声明为可选,这意味着它最初为零,并且将来可能没有值。 或者您可以在此处初始化变量,如下所示。

选项:

var student: Student = Student() { willSet {...} }

但是在这里你使用的是willSet{}这意味着你获得的是动态值,而不是编译时间。 所以我认为第一个选项将删除您的错误。 您应该尝试最适合您的最佳选择。

结论 我们必须初始化一个变量。

You should try these:

Option 1:

var student: Student! { willSet {...} }

Option 2:

var student: Student? { willSet {...} }

because we must init a variable otherwise compiler will give error. Here in option 1 we have declared this variable as implicitly unwrapped optional. It means it's initial value is nil but it's value will not be nil in future. In option 2 we have declared this variable as optional it means initially its nil and in future it might not have a value. Or you can init a variable here as below.

Option :

var student: Student = Student() { willSet {...} }

But here you are using willSet{} it means you are getting dynamically value than rather than compile time. So I think first option will remove your error. You should try the best option that suits you best.

Conclusion We must init a variable.

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/697360.html

相关标签:无
上传时间: 2023-08-27 09:43:41
留言与评论(共有 12 条评论)
本站网友 天津婚礼策划
10分钟前 发表
UITableViewCell { override func awakeFromib() { super.awakeFromib() // Initialization code } override func setSelected(_ selected
本站网友 睡眠面膜有效果吗
14分钟前 发表
编译器开始抱怨Class 'MyCell' has no initializers
本站网友 海安租房
9分钟前 发表
compiler started complaining class has no initializer) 我创建了一个继承UITableViewCell的类: class MyCell
本站网友 驴象之争
2分钟前 发表
否则编译器会给出错误
本站网友 4万亿投资
6分钟前 发表
Student? { willSet {...} } 因为我们必须初始化变量
本站网友 你喔
0秒前 发表
Bool
本站网友 南澳大利亚
10分钟前 发表
UITableViewCell { var student
本站网友 中海苏黎世家
16分钟前 发表
Bool) { super.setSelected(selected
本站网友 怎么胎教
9分钟前 发表
Student{ willSet {...} } ... } ow
本站网友 飞扬影城
3分钟前 发表
compiler started complaining class has no initializer) 我创建了一个继承UITableViewCell的类: class MyCell
本站网友 变速齿轮无广告
11分钟前 发表
在选项2中