swift 的init_Swift init()

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())

Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.
初始化是准备使用的类,结构或枚举实例的过程。 此过程涉及为该实例上的每个存储属性设置一个初始值,并执行新实例准备使用之前所需的任何其他设置或初始化。

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:

这给我们提供了两种可能的选择:

  1. Assign a default property value in the property definition itself.

    在属性定义本身中分配默认属性值。
  2. 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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值