您的位置:首页 > 产品设计 > UI/UE

stored property "text" without initial value prevents synthesized initializers

2016-07-26 21:56 996 查看

swift开发时有可能会遇到这样的错误提示

class ARandom{
var number: Int = 0
var text: String
}


stored property “text” without initial value prevents synthesized initializers

看了下文档,有这样的解释:

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. These actions are described in the following sections.

意思大概就是class和struct的property必须要在实例化之前赋值。那么解决方案就是要赋值或者把变量声明为optional:

class ARandom{
var number: Int = 0
var text: String?
}

//or
class ARandom{
var number: Int = 0
var text: String = ""
}

//or
class ARandom{
var number: Int = 0
var text: String

init(){
text =""
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  swift iOS