使用()和init()创建对象有什么区别?

Difficulty: Beginner | Easy | Normal | ChallengingThis article has been developed using Xcode 11.5, and Swift 5.2.4

难度:初学者| 容易| 普通| 具有挑战性本文是使用Xcode 11.5和Swift 5.2.4开发的

先决条件: (Prerequisites:)

问题 (The Question)

You might have seen the two alternatives that follow for instantiating a class: Person(name: "Dave") and Person.init(name: "Dave") . What is the difference, and which should we use?

您可能已经看到用于实例化class以下两种选择: Person(name: "Dave")Person.init(name: "Dave") 。 有什么区别,我们应该使用哪个?

This article seeks to give the solution

本文寻求给出解决方案

一个例子 (An Example)

Imagine you have a struct. This struct is going to store a person's name.

假设您有一个struct 。 这个struct将存储一个人的名字。

If you need a reminder about initializers please do take a look at this guide . I'll wait.

如果您需要有关初始化的提示,请仔细阅读本指南 。 我会等。

Are you back?

你回来了吗?

Right.

对。

This struct can be created in Swift by using:

可以使用以下方法在Swift中创建此结构:

Image for post

now there are two alternative ways to create an instance of this object in Swift:

现在有两种方法可以在Swift中创建此对象的实例:

Image for post

now this means the same thing. If you print out the two struct instances defined, exactly the same thing will be printed.

现在,这意味着同一件事 。 如果打印出定义的两个struct实例,则将打印完全相同的内容。

When we say that this is a question of style. Now in Swift we usually use the former initializer style, that is Person(name: "Dave") if we wish to intiialize a person with the name Dave, rather than Person.init(name: "Dave") . Now the reason behind this is that it resembles the constructor syntax in other languages - great!

当我们说这是风格问题时。 现在,在Swift中,如果我们希望初始化一个名称为Dave而不是Person.init(name: "Dave")的人,那么我们通常使用以前的初始化样式,即Person(name: "Dave") Person.init(name: "Dave") 。 现在的原因是它类似于其他语言的构造函数语法-太好了!

Does the same apply to class declarations? Of course, yes it does!

同样适用于class声明吗? 当然可以!

Now you do need to create your own initializer since this is a class in Swift. No problem for the more experienced of you:

现在您确实需要创建自己的初始化程序,因为这是Swift中的class 。 对于经验丰富的人来说没问题:

Image for post

If you print out the two class instances defined, exactly the same thing will be printed.

如果打印出已定义的两个class实例,则将打印完全相同的内容。

使用.init()的原因 (Reasons for using .init())

So I’ve just said that it’s a question of style. But there are other reasons that you might use .init()

所以我刚刚说过,这是一个风格问题。 但是还有其他原因可能需要使用.init()

You can use Swift’s great type inference to make the instantiation of an Array somewhat easier - by using .init(). Look at the following example:

您可以使用Swift的出色类型推断,通过使用.init().init() Array的实例化。 看下面的例子:

Image for post

Now it is arguable (and you can tell me I’m wrong on Twitter ) that the second is better in terms of understanding — particularly with more complex classes (and I DO appreciate that I’ve picked a rather simple example here).

现在可以争论(并且您可以告诉我,我在Twitter上错了),第二点在理解方面更好-特别是对于更复杂的类(并且我确实很欣赏我在这里选择的一个简单示例)。

Another example, using mapping in Swift allows us to convert a String into an array of String (that is, [String]) Here are two possible solutions to this problem:

另一个示例, 在Swift中使用映射使我们可以将String转换为String (即[String] )数组。以下是此问题的两种可能的解决方案:

Image for post

Now you can see that the first option initializing with .init() would be a nicer option.

现在您可以看到,使用.init()初始化的第一个选项将是一个更好的选择。

Although, yes it is true this once again comes down to style.

虽然,是的,这是事实,这再次归结为风格。

结论 (Conclusion)

You might well think of style in programming to be an inessential nice-to-have that you can get away without considering. I’d say that isn’t since the point in studying a language is to work with other people, and perhaps even creating great software that meets business needs.

您可能会认为编程中的样式是您不必考虑就可以摆脱的无关紧要的必备条件。 我要说的并不是因为学习语言的重点是与其他人合作,甚至可能创建满足业务需求的出色软件。

In order to do just that…you’ll need to think of your programming style. Isn’t it time you thought about that?

为了做到这一点……您需要考虑自己的编程风格。 你不是时候考虑这个吗?

If you’ve any questions, comments or suggestions please hit me up on Twitter

如果您有任何疑问,意见或建议,请在Twitter上打我

翻译自: https://medium.com/@stevenpcurtis.sc/whats-the-difference-in-creating-an-object-with-and-init-5fedd0ea83ad

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值