class ARandom{ var number: Int = 0 var text: String }
但是,Xcode显示以下错误:
stored property "text" without initial value prevents synthesized initializers
为什么会发生这种情况?什么是合成初始化器?为什么没有初始值的“文本”会阻止启动启动?有人可以请我向我解释一下吗?提前提供任何帮助!
>使文本可选.
var text:String?
>给文本一个默认值
var text:String =“”
>在ARandom的初始化程序中给文本一个值
init(){text =“”}
发生这种情况的原因是您将文本定义为String.它不是可选的.基本上你说的是它总是一个String,永远不会是零.
使用您当前的代码,如果您创建了一个新的实例的ARandom,文本将没有价值 – 如果文本不可选,那是不可能的
苹果的文档可能会解释一下好一点
Classes and structures must set all of their stored properties to an
appropriate initial value by the time an instance of that class or
structure is created. Stored properties cannot be left in an
indeterminate state.You can set an initial value for a stored property within an initializer,or by assigning a default property value as part of the property’s definition.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。