如何在 Swift 中使用初始化器或构造函数
How to use initialisers or a constructor in Swift
我一直对 Swift 中的基本要素有疑问。当我编码时,我经常花太多时间在 类 中使用变量和常量。我认为我挣扎的原因是因为我植根于 Java,那里的一切几乎都是在构造函数中创建的,所以 Class myVar = new Class()
对我来说真的很熟悉,但在 Swift 中,我查看两者:
let userClient = UserClient()
let userService: UserService
...
init(userService: UserService) {
self.userService = userService
}
在我看来,这有点像将某物传递给多个 类?
Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.
我明白了。伟大的。但是为什么我们不总是声明像 let iNeedThisFunctionality = Functionality()
.
这样的东西
抱歉,如果这个想法真的很基础,我可以创建复杂的 UI 和用户身份验证等等,这让我很恼火,但我一直在这个问题上犯错误。我有点依赖 xcode 来告诉我我声明了错误。
谢谢!
这与它是存储的 属性 还是计算的 属性 有关。有关详细信息,请参阅此文档:https://docs.swift.org/swift-book/LanguageGuide/Initialization.html
I'm rooted in Java where everything is pretty much created in a constructor
如果需要,您可以在初始化程序中执行所有操作:
class Foo {
let userClient: UserClient
let userService: UserService
init(userService: UserService) {
self.userService = userService
self.userClient = UserClient()
}
}
只是 userClient
的初始化不依赖于任何初始化参数,或 self
,所以它可以内联完成:
let userClient = UserClient()
But why don't we always just declare things like let iNeedThisFunctionality = Functionality()
?
您不能以这种方式初始化 userService
,因为它需要初始化为 initialiser 参数。即当您创建 Foo
:
let foo = Foo(userService: someService)
foo.userService
需要 someService
。 Foo
的设计要求创建一个Foo
,你需要传递一个UserService
给它,它的userService
属性会被初始化为那个服务.
如果你是“rooted in Java”,你也应该在Java看到过这样的代码:
public class Foo {
private final UserClient userClient = new UserClient();
private final UserService userService;
public Foo(UserService userService) {
this.userService = userService;
}
}
这就是我将您的 Swift 代码翻译成 Java 的方式。你也可以“两者兼顾”。
我一直对 Swift 中的基本要素有疑问。当我编码时,我经常花太多时间在 类 中使用变量和常量。我认为我挣扎的原因是因为我植根于 Java,那里的一切几乎都是在构造函数中创建的,所以 Class myVar = new Class()
对我来说真的很熟悉,但在 Swift 中,我查看两者:
let userClient = UserClient()
let userService: UserService
...
init(userService: UserService) {
self.userService = userService
}
在我看来,这有点像将某物传递给多个 类?
Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.
我明白了。伟大的。但是为什么我们不总是声明像 let iNeedThisFunctionality = Functionality()
.
抱歉,如果这个想法真的很基础,我可以创建复杂的 UI 和用户身份验证等等,这让我很恼火,但我一直在这个问题上犯错误。我有点依赖 xcode 来告诉我我声明了错误。
谢谢!
这与它是存储的 属性 还是计算的 属性 有关。有关详细信息,请参阅此文档:https://docs.swift.org/swift-book/LanguageGuide/Initialization.html
I'm rooted in Java where everything is pretty much created in a constructor
如果需要,您可以在初始化程序中执行所有操作:
class Foo {
let userClient: UserClient
let userService: UserService
init(userService: UserService) {
self.userService = userService
self.userClient = UserClient()
}
}
只是 userClient
的初始化不依赖于任何初始化参数,或 self
,所以它可以内联完成:
let userClient = UserClient()
But why don't we always just declare things like
let iNeedThisFunctionality = Functionality()
?
您不能以这种方式初始化 userService
,因为它需要初始化为 initialiser 参数。即当您创建 Foo
:
let foo = Foo(userService: someService)
foo.userService
需要 someService
。 Foo
的设计要求创建一个Foo
,你需要传递一个UserService
给它,它的userService
属性会被初始化为那个服务.
如果你是“rooted in Java”,你也应该在Java看到过这样的代码:
public class Foo {
private final UserClient userClient = new UserClient();
private final UserService userService;
public Foo(UserService userService) {
this.userService = userService;
}
}
这就是我将您的 Swift 代码翻译成 Java 的方式。你也可以“两者兼顾”。