Swift中的self类型及其在两阶段初始化中的使用 [英] The type of self in Swift and its use with respect to two-phase initialization
问题描述
考虑以下代码,该代码将手势识别器添加到视图中.
Consider the following code, which adds a gesture recognizer to a view.
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
let gesture = UITapGestureRecognizer(target: self, action: #selector(handleGesture(gesture:)))
let test1 = self
@objc func handleGesture(gesture: UITapGestureRecognizer) {
// some code
print("hello")
}
override func viewDidLoad() {
let test2 = self
super.viewDidLoad()
imageView.addGestureRecognizer(gesture)
}
}
根据此问题,上面的代码不起作用,因为我试图在未完全初始化时使用self
(在手势识别器的初始化程序中),这是因为Swift的两阶段初始化.
As per this question, the above code does not work because I'm trying to use self
(in the gesture recognizer's initializer) when not fully initialized, and this is so because of Swift's two-phase initialization.
我对实现此功能的简单解决方法不感兴趣,但这引发了两个问题:
I'm not interested in the easy fix to make this work, but this triggers a couple of questions:
1)如果尚未准备好使用self
,为什么编译器允许我们在此处使用self
?如果我尝试过早使用self
,我是否应该得到编译器错误?
1) Why does the compiler allow us to use self
here if self
is not ready to be used? Shouldn't I get a compiler error if I'm trying to use self
too soon?
2)我们无法使用alt +在XCode中单击直接检查self
的类型.但是,我们可以检查临时变量test1
和test2
的类型.当test2
的类型为ViewController
时,正如预期的那样,test1
的类型为(ViewController) -> () -> ViewController
(即,一个采用ViewController
的闭包,返回不采取任何操作并返回ViewController
的闭包) .那是什么?为什么self
在同一类中有两种不同的类型?
2) We can't directly inspect the type of self
with alt+click in XCode. However, we can inspect the types of my ad hoc variables test1
and test2
. While test2
's type is ViewController
, as expected, test1
's type is (ViewController) -> () -> ViewController
(i.e., a closure that takes a ViewController
and returns a closure that takes nothing and returns a ViewController
). What is that and why does self
have two different types within the same class?
推荐答案
1)
如果我尝试过早使用self
会不会出现编译器错误?
Shouldn't I get a compiler error if I'm trying to use self
too soon?
我同意.您可以将错误报告发送到swift.org .
如果尚未准备好使用self
,为什么编译器为什么允许我们在此处使用self
?
Why does the compiler allow us to use self
here if self
is not ready to be used?
不幸的是,在NSObject
的后代中还有另一个self
, NSObject
的方法self()
.
Unfortunately, there's another self
in the descendants of NSObject
, the method self()
of NSObject
.
2)
这是什么,为什么自我在同一个类中有两种不同的类型?
What is that and why does self have two different types within the same class?
当前的Swift在class
上下文中而不是在实例上下文中解释初始值表达式.
The current Swift interprets the initial value expression in the class
context, not in the instance context.
您知道方法名称可以在Swift中用作闭包:
You know method names can be used as closures in Swift:
class ViewController: UIViewController {
//..
func aMethod() {
//...
}
func anInstanceMethod() {
let meth = aMethod // () -> ()
}
}
Swift也可以在class
上下文中引用实例方法,该实例方法生成所谓的未应用方法引用(请参见
Swift can also refer to an instance method in the class
context, which generates a so-called unapplied method reference (see SE-0042), which currently returns a curried function:
class ViewController: UIViewController {
//...
func aMethod() {
//...
}
class func aClassMethod() {
let meth = aMethod // (ViewController) -> () -> ()
}
}
方法self()
也是如此.
一般来说,我们不需要self()
方法,我认为应该更改这种行为.
Generally we do not need self()
method and this behavior should be changed, I think.
这篇关于Swift中的self类型及其在两阶段初始化中的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!