在init中解决在闭包内创建的实例

时间:2018-03-28 07:09:51

标签: ios swift reference initialization closures

我有一个类ControlPanel,需要在初始化期间对其UI元素进行一些调整。我希望将这些调整作为一个闭包传递给它init()。但是,我如何正确引用self?这样做的时候......

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

... Swift当然假设self指的是我调用此初始值设定项的类。这完全不相关。是否有theSelfWhenThisClosureIsBeingExecuted等语法?

我的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)
}

1 个答案:

答案 0 :(得分:0)

你可以将setup声明为一个关闭,它接受刚创建的 instance作为参数。例如:

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")
    }
}

然后可以将其称为

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

但是,只能在实例时调用闭包 完全初始化,这意味着someProperty需要 一个变量,,必须分配一个初始值。 因此,创建后进行自定义设置没有太大的优势 实例:

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