ios 从初始化程序返回而不初始化所有存储的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29673488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return from initializer without initializing all stored properties
提问by Isuru
I have a simple class like this.
我有一个像这样的简单类。
public class User {
let id: Int
let firstName: String
let lastName: String
let email: String?
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
This compiled just fine in previous Swift version. In Swift 1.2, I get the following compilation error.
这在以前的 Swift 版本中编译得很好。在 Swift 1.2 中,我收到以下编译错误。
Return from initializer without initializing all stored properties
从初始化程序返回而不初始化所有存储的属性
Why is that and how can I resolve it?
为什么会这样,我该如何解决?
回答by Dániel Nagy
If a property is constant, so created with let
, you have to initialize it in place or in the init
method, even if it is an Optional
. If you want to be able to set email optionally, you should change let
to var
. In other words, if you are not initializing a variable in either the init method or class body, then the variable must be both a var
and an Optional
.
如果一个属性是常量,所以用 来创建let
,你必须就地或在init
方法中初始化它,即使它是一个Optional
. 如果您希望能够选择性地设置电子邮件,则应更改let
为var
. 换句话说,如果您没有在 init 方法或类主体中初始化变量,则该变量必须同时是 avar
和 an Optional
。
Related statements in the docs:
文档中的相关声明:
You can assign a value to a constant property at any point during initialization, as long as it is set to a definite value by the time initialization finishes. Once a constant property is assigned a value, it can't be further modified.
For class instances, a constant property can only be modified during initialization by the class that introduces it. It cannot be modified by a subclass.
您可以在初始化期间的任何时候为常量属性赋值,只要在初始化完成时将其设置为确定值即可。一旦为常量属性分配了一个值,就不能再对其进行修改。
对于类实例,常量属性只能在初始化期间由引入它的类修改。它不能被子类修改。
回答by Tiago Almeida
The rule of thumb is:
经验法则是:
- If you have let, you need to initialize it (even if it is optional). Eg :
let email: String
- If you have a non optional varyou need to initialize. Eg
var email: String
- If you have an optional var you don't need to initialize it. Eg
var email: String?
- 如果你有让,你需要初始化它(即使它是可选的)。例如:
let email: String
- 如果你有一个非可选的 var你需要初始化。例如
var email: String
- 如果你有一个可选的 var 你不需要初始化它。例如
var email: String?
回答by Onato
You need to explicitly set a value for email since it is a constant.
您需要为电子邮件明确设置一个值,因为它是一个常量。
public class User {
let id: Int
let firstName: String
let lastName: String
let email: String?
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
self.email = nil // <--------------
}
}
Or as the others mentioned you can change email to a variable.
或者正如其他人提到的,您可以将电子邮件更改为变量。
回答by Chamath Jeevan
Change the email from "let" to "var" as below.
将电子邮件从“let”更改为“var”,如下所示。
public class User {
let id: Int
let firstName: String
let lastName: String
var email: String?
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
回答by Saad
First of all make it clear that every let variable must be assigned value at declaration time. So your statements will be
首先明确每个 let 变量必须在声明时赋值。所以你的陈述将是
let id: Int = 0
let firstName: String = "test"
let lastName: String = "Test"
let email: String? = "Test"
Secondly in classes, you must have to initialize variables or define them as optional types by either putting '?' or '!' with every variable. Like
其次,在类中,您必须初始化变量或通过放置 '?' 将它们定义为可选类型。或者 '!' 与每个变量。喜欢
let id: Int!
let firstName: String!
let lastName: String!
let email: String?
or
或者
let id: Int?
let firstName: String?
let lastName: String?
let email: String?
But want to say you here that these variables will not be able to change as they are constants. so you must use var with these if you'r not passing value at time of declaration. Your final code in this case will be some kind of this
但是想在这里告诉你,这些变量将无法更改,因为它们是常量。因此,如果您在声明时未传递值,则必须将 var 与这些一起使用。在这种情况下,您的最终代码将是某种
var id: Int
var firstName: String
var lastName: String
var email: String?