swift 构造函数_Swift快捷键#1-在结构中添加新的构造函数

swift 构造函数

Swift中的结构 (Structs in Swift)

In Swift, structs are (or should be) the go-to abstraction used to store properties or methods. This is due to many aspects, such as their value-type semantics.

在Swift中,结构是(或应该是)用于存储属性或方法的抽象方法。 这是由于许多方面,例如它们的值类型语义。

One of the most interesting aspects of Swift structs is that they enable the compiler to auto-synthesize constructors (or initializers).

Swift结构最有趣的方面之一是它们使编译器能够自动合成构造函数(或初始化程序)。

For example, below we define a struct Person with a property name and use the compiler-generated initializer to create a new instance.

例如,下面我们定义一个带有属性名称的struct Person ,并使用编译器生成的初始化程序创建一个新实例。

定义新构造函数的问题 (The problem with defining new Constructors)

Compiler-generated init functions are great but what if we add our own initializer? A no-paramater initializer, for example. The simplest way to do so is define an custom init inside the declaration of the struct.

编译器生成的init函数很棒,但是如果我们添加自己的初始化器呢? 例如,无参数的初始化程序。 最简单的方法是在结构的声明中定义一个自定义init。

Image for post

The bad part of this approach is that now that we have defined our own initializer, the compiler has not been able to auto-generate the default init function. Thus, our struct has only one valid initialization method, the one we defined.

这种方法的坏处在于,既然我们已经定义了自己的初始化程序,则编译器无法自动生成默认的初始化函数。 因此,我们的结构只有一种有效的初始化方法,即我们定义的一种。

Image for post

This particular case of empty initialization can be solved by setting default values to our properties. This was, we can have two initializer provided by the compiler.

可以通过将默认值设置为属性来解决这种空初始化的特殊情况。 这样,我们可以有两个由编译器提供的初始化程序。

Image for post

添加新的构造函数 (Adding new Constructors)

Now this are still auto-generated inits, what if we actually want to add a new constructor that receives parameters, can we still keep the compiler-generated init functions?

现在这仍然是自动生成的init ,如果我们实际上想添加一个新的接收参数的构造函数,又可以保留编译器生成的init函数呢?

Well, turns out we can! Swift allows us to add new constructors to structs and still keep the compiler-generated ones by using extensions.

好吧,事实证明我们可以! Swift允许我们向结构中添加新的构造函数,并且仍然通过使用扩展来保留编译器生成的构造函数。

If we extend a struct and define a custom initializer, the compiler can synthesize the default initializers and we get our custom constructor.

如果扩展结构并定义自定义初始化程序,则编译器可以合成默认的初始化程序,并获得自定义构造函数。

In our example, we can add a custom init.

在我们的示例中,我们可以添加一个自定义init。

Image for post

And now we have three ways to initialize our struct!

现在,我们有三种初始化结构的方法!

Image for post

As a result, our final implementation is presented below.

因此,下面介绍了我们的最终实现。

Image for post

摘要 (Summary)

One of the nicest features of structs in Swift is they can have their initializers generated by the compiler. We often want to add our own init function thought, but keeping these auto-generated inits can be quite useful. As such, a great way to have custom constructors while maintaining the compiler-generated ones is through extensions.

Swift中结构最好的功能之一是它们可以由编译器生成其初始化程序。 我们经常要添加自己的初始化函数的思想,但保持这些自动生成的inits是非常有用的。 因此,拥有自定义构造函数同时维护编译器生成的构造函数的一种好方法是通过扩展。

If you want to see this feature in action through a video, check out the links below.

如果您想通过视频观看此功能,请查看以下链接。

翻译自: https://medium.com/academy-poa/swift-shortcuts-1-adding-new-constructors-to-structs-e452d5ff8b2d

swift 构造函数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值