简体   繁体   English

在init的闭包内部创建的地址实例

[英]Adressing instance created inside closure in init

I have a Class ControlPanel that needs to make some adjustments to its UI Elements during initialization. 我有一个ControlPanel类,需要在初始化期间对其UI元素进行一些调整。 I want to pass those adjustments as a closure in it's init() . 我想将这些调整作为init()的闭包进行传递。 But how do I correctly reference self ? 但是我该如何正确地引用self呢? When doing this... 这样做时...

let cornerRadius = ControlPanel(title: "Corner Radius",
                                        layout: .oneSlider,
                                        sliderRange: (0.0, 30.0),
                                        setup: { self.someProperty = 5 }
        )

...Swift of course assumes self refers to the class in which I am calling this initializer. ...迅捷当然假设self是指我在其中调用此初始化程序的类。 Which is totally unrelated. 这完全无关。 Is there any syntax like theSelfWhenThisClosureIsBeingExecuted ? 是否有类似theSelfWhenThisClosureIsBeingExecuted语法?

My ControlPanel init is defined like this: 我的ControlPanel初始化定义如下:

init(title: String, layout: PanelControlLayoutType, sliderRange: (min: Float, max: Float), editClosure1of1: ((Float) -> ())?, setup: () -> ()) {
    super.init(frame: CGRect())
    commonInit(title: title)
    guard layout == .oneSlider else { fatalError("Wrong paneltype") }
    let singleSlider = UISlider()
    slider1?[0] = singleSlider
    singleSlider.minimumValue = sliderRange.min
    singleSlider.maximumValue = sliderRange.max
    self.addSubview(singleSlider)
    self.editClosure1of1 = editClosure1of1
    singleSlider.translatesAutoresizingMaskIntoConstraints = false
    singleSlider.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 50.0).isActive = true
    singleSlider.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -50.0).isActive = true
    singleSlider.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -32.0).isActive = true
    singleSlider.tag = 0
    singleSlider.addTarget(self, action: #selector(self.sliderChanged(_:withEvent:)), for: .valueChanged)
}

You could declare setup as a closure which takes the just created instance as parameter. 您可以将setup声明为一个闭包,它使用刚创建的实例作为参数。 Example: 例:

class ControlPanel: UIView {

    let title: String
    var someProperty: Int = 0

    init(title: String, setup: ((ControlPanel) -> Void)? = nil) {
        self.title = title
        super.init(frame: CGRect())

        if let setup = setup {
            setup(self)
        }
    }

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

This can then be called as 这可以称为

let panel = ControlPanel(title: "title") {
    $0.someProperty = 5
}

However, the closure can only be called when the instance is fully initialized, which means that someProperty needs to be a variable, and must be assigned an initial value. 但是,只有在实例完全初始化后才能调用闭包,这意味着someProperty需要是一个变量,并且必须分配一个初始值。 So there is not much advantage over doing the custom setup after creating the instance: 因此,创建实例后进行自定义设置没有太多优势:

let panel = ControlPanel(title: "title")
panel.someProperty = 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM