swift 的init
In this Swift tutorial, we’ll be discussing an important concept, namely Swift init or Swift initialization. Initialization is what happens when we create an instance of some type.
在本Swift教程中,我们将讨论一个重要的概念,即Swift初始化或Swift初始化。 当我们创建某种类型的实例时,就会发生初始化。
Swift init() (Swift init())
Initializers are similar to constructors in java programming. Swift being a type-safe language has placed a lot of rules for initializers. It can get tricky to implement unless you’ve got a good hold of the concept.
初始化程序类似于Java编程中的构造函数。 Swift是一种类型安全的语言,它为初始化程序设置了很多规则。 除非您很好地理解这个概念,否则可能很难实现。
Swift init()语法 (Swift init() syntax)
init() {
// initialise the stored properties here.
}
Let’s look at a sample class below.
让我们看下面的示例类。
class A{
//Compilation error. No initializer is defined.
var a : Int
var b : String
var c : Int?
let website = "JournalDev"
}
Above class won’t compile. The swift compiler complains that the stored properties aren’t initialized. Stored Properties can’t be kept in an undetermined state.
上面的类不会编译。 Swift的编译器抱怨存储的属性未初始化。 存储的属性不能保持不确定状态。
This leaves us with two possible options:
这给我们提供了两种可能的选择:
- Assign a default property value in the property definition itself. 在属性定义本身中分配默认属性值。
- Use an initializer,
init()
for initializing the properties. 使用初始化程序init()
初始化属性。
Let’s look at each of the approaches one at a time.
让我们一次看看每种方法。
class A{
var a : Int = 5
var b : String = "Hello. How you're doing"
var c : Int?
let website = "JournalDev"
}
Here we’ve set a default value for each of the stored properties, hence Swift provides us the default initializer implicitly. All the properties and functions can be accessed using the dot operator over an instance of the class once it’s initialized.
在这里,我们为每个存储的属性设置了默认值,因此Swift隐式地为我们提供了默认初始化器。 初始化类后,可以使用点运算符在类的实例上访问所有属性和函数。
var object = A()
object.a = 10
object.c = 2
The second way is to initialize the stored properties using the init()
method as shown below.
第二种方法是使用init()
方法初始化存储的属性,如下所示。
class A{
var a : Int
var b : String
var c : Int?
let website = "JournalDev"
init(a: Int, b: String) {
self.a = a
self.b = b
}
}
var object = A(a: 5, b: "Hello World")
Note: Swift Optional is not a stored properties. Hence, they need not be initialized.
注意 : Swift Optional不是存储的属性。 因此,它们不需要初始化。
Stored properties are accessed inside the init()
method using self
property.
使用self
属性可以在init()
方法内部访问存储的属性。
Note: self
is used to refer to the current instance within its own instance methods (Similar to this
in java).
The above initializer is the primary initializer of the class. It’s also known as designated initializer(we’ll discuss this later).
注 : self
被用来指代当前实例自身的实例方法(类似于内this
在Java)。
上面的初始化器是该类的主要初始化器。 也称为指定的初始化程序 (我们将在后面讨论)。
Initializers lets us modify a constant property too.
通过初始化程序,我们也可以修改常量属性。
class A{
var a : Int
var b : String
var c : Int?
let website : String
init(a: Int, b: String, website: String) {
self.a = a
self.b = b
self.website = website
}
}
var object = A(a: 5,b: "Hello World", website: "JournalDev")
结构的成员初始化器 (Memberwise Initializers for Structures)
Structures being value types, don’t neccessarily require an initializer defined. Structure types automatically receive a memberwise initializer unless you’ve defined custom initializer(s).