gpt4 book ai didi

swift - 在 super.init 中引用自己

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:57 24 4
gpt4 key购买 nike

我有以下代码(编辑:更新代码以便每个人都可以编译并查看):

import UIKit

struct Action
{
let text: String
let handler: (() -> Void)?
}

class AlertView : UIView
{
init(actions: [Action]) {
super.init(frame: .zero)

for action in actions {
// let actionButton = ActionButton(type: .custom)
// actionButton.title = action.title
// actionButton.handler = action.handler
// addSubview(actionButton)
}
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

class TextAlertView : AlertView
{
init() {
super.init(actions: [
Action(text: "No", handler: nil),
Action(text: "Yes", handler: { [weak self] in
//use self in here..
})
])
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

class MyViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

let alert = TextAlertView()
view.addSubview(alert)
self.view = view
}
}

每次我实例化 TextAlertView 时,它都会因访问错误而在 super.init 上崩溃。但是,如果我改变:

Action(title: "Yes", { [weak self] in
//use self in here..
})

到:

Action(title: "Yes", {
//Blank.. doesn't reference `self` in any way (weak, unowned, etc)
})

它有效!

有没有一种方法可以在 super 初始化期间引用 self,无论它是弱还是不在操作 block 内(在上面我在 super.init 的参数中这样做>?

代码编译..它只是在运行时随机崩溃。

最佳答案

简答:

super.init 返回之前,您不能捕获并使用 self 作为值。在您的情况下,您正试图“通过” selfsuper.init 作为参数。

至于为什么第二部分有效,仅仅是因为在其中没有使用self,它没有捕获self,因此它没有使用self 作为一个值。

如果你不想在闭包中使用self,那么你不需要担心strong/weak引用在那里,因为那里根本没有对 self 的引用(因为它没有被捕获)。没有保留循环的危险。


关于“使用 self 作为值”的简短旁注 - 您可以在赋值的左侧使用 self 来引用 的属性self 初始化它们时:

let myProperty: String

init(with myProperty: String) {
// this usage of self is allowed
self.myProperty = myProperty
super.init(nibName: nil, bundle: nil)
}

带有引用和内容的更长答案:

根据 documentation :

Safety check 4

An initializer cannot call any instance methods, read the values of any instance properties, or refer to self as a value until after the first phase of initialization is complete.

初始化的第一阶段通过调用super.init结束,当

来自同一文档:

Phase 1

A designated or convenience initializer is called on a class.

Memory for a new instance of that class is allocated. The memory is not yet initialized.

A designated initializer for that class confirms that all stored properties introduced by that class have a value. The memory for these stored properties is now initialized.

The designated initializer hands off to a superclass initializer to perform the same task for its own stored properties.

This continues up the class inheritance chain until the top of the chain is reached.

Once the top of the chain is reached, and the final class in the chain has ensured that all of its stored properties have a value, the instance’s memory is considered to be fully initialized, and phase 1 is complete.

因此只有在调用 super.init 之后,您才可以使用 self 作为值:

Phase 2

Working back down from the top of the chain, each designated initializer in the chain has the option to customize the instance further. Initializers are now able to access self and can modify its properties, call its instance methods, and so on.

Finally, any convenience initializers in the chain have the option to customize the instance and to work with self.

现在,当您尝试将 self 用作闭包捕获列表中的值时,它会崩溃,对此我一点都不感到惊讶。更令我惊讶的是编译器确实允许您这样做 - 现在我猜这是一种未实现错误处理的边缘情况。

第二种情况:

Action(title: "Yes", {
//Blank.. doesn't reference `self` in any way (weak, unowned, etc)
})

您并没有真正捕获self,这就是它被允许并且有效的原因。但是您无权访问 self 。尝试在其中添加一些使用 self 的代码,编译器会报错:

enter image description here

所以最后,如果你想在闭包中使用self,你将不得不找到一种方法如何首先调用super.init,然后才调用将 self 捕获闭包添加到属性中。

关于swift - 在 super.init 中引用自己,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48996228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com