在Swift 1.2中将self作为参数传递给init方法 [英] Pass self as argument within init method in Swift 1.2

查看:152
本文介绍了在Swift 1.2中将self作为参数传递给init方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下类将' let '属性声明为隐式展开的变量。这之前使用过Xcode 6.2:

The following class has a 'let' property declared as implicitly unwrapped variable. This previously worked with Xcode 6.2:

class SubView: UIView {

    let pandGestureRecognizer: UIPanGestureRecognizer!

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.pandGestureRecognizer = UIPanGestureRecognizer(target: self, action: "panAction:")
    }

    func panAction(gesture: UIPanGestureRecognizer) {
        // ...
    }
}

更新到Xcode 6.3(使用Swift 1.2)后,会发生以下编译错误:

After updating to Xcode 6.3 (with Swift 1.2), the following compilation errors occur:

Property 'self.panGestureRecognizer' not initialized at super.init call
Immutable value 'self.panGestureRecognizer' may only be initialized once

super.init 之前移动以下行:

self.pandGestureRecognizer = UIPanGestureRecognizer(target: self, action: "panAction:")

给出以下错误:

'self' is used before super.init call

property' panGestureRecognizer '不需要变异,因此必须将其声明为常量' let '。由于它是常量,因此必须在声明时具有初始值,或者在 init 方法中初始化它。要初始化它,需要在' target '参数中传递' self '。

The property 'panGestureRecognizer' requires no mutation, therefore it has to be declared as constant 'let'. Since it is a constant, it has to have an initial value upon declaration, or initialize it within the init method. To initialize it, it requires to pass 'self' in the 'target' parameter.

其他线程建议将其声明为隐式解包可选,并在 super.init 调用后初始化它。这之前一直有效,直到我更新到Xcode 6.3。

Other thread suggested to declare it as implicitly unwrapped optional, and initialize it after the super.init call. This previously worked until I updated to Xcode 6.3.

有没有人知道这种情况的正确实现或解决方法?

Does anybody know a proper implementation or a workaround for this case?

推荐答案

问题

问题是您使用 - 声明为 let 的选项不会被赋予默认值 nil (<$然而,c $ c> var 。以下是在Swift 1.2中引入的,否则无效,因为在声明之后你将无法给 myOptional 一个值:

The problem is your use of let - optionals declared as let aren't given a default value of nil (var is however). The following, introduced in Swift 1.2, wouldn't be valid otherwise since you wouldn't be able to give myOptional a value after declaring it:

let myOptional: Int?

if myCondition {
    myOptional = 1
} else {
    myOptional = nil
}

因此,您在调用 super之前,会收到错误'Property'self.panGestureRecognizer'未在super.init调用中初始化'。 init(coder:aDecoder),因为 panGestureRecognizer 不是 nil ;它根本没有初始化。

Therefore, you're getting the error 'Property 'self.panGestureRecognizer' not initialized at super.init call' because before calling super.init(coder: aDecoder), because panGestureRecognizer isn't nil; it hasn't been initialised at all.

解决方案:

1。声明 panGestureRecognizer var ,这意味着它将被赋予默认值 nil ,您可以在调用 super.init(编码器:aDecoder)后更改。

1. Declare panGestureRecognizer as a var, meaning it will be given a default value of nil, which you could then change after calling super.init(coder: aDecoder).

2。在我看来,更好的解决方案是:不要使用隐式解包的可选项并声明 panGestureRecognizer 初始值 UIPanGestureRecognizer()。然后在调用 super.init 之后设置目​​标:

2. In my opinion, the better solution: don't use an implicitly unwrapped optional and declare panGestureRecognizer with an initial value of UIPanGestureRecognizer(). Then set the target after super.init is called:

class SubView: UIView {
    let panGestureRecognizer = UIPanGestureRecognizer()

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        panGestureRecognizer.addTarget(self, action: Selector("panAction:"))
    }
}

这篇关于在Swift 1.2中将self作为参数传递给init方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆