升级到Swift 3后,属性'self.delegate'未在super.init调用时初始化 [英] Property 'self.delegate' not initialized at super.init call after upgrading to Swift 3

查看:608
本文介绍了升级到Swift 3后,属性'self.delegate'未在super.init调用时初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到Swift 3后,我现在收到此错误:

After upgrading to Swift 3 I now get this error:


属性'self.delegate'未在super.init调用时初始化

Property 'self.delegate' not initialized at super.init call

在NSObject类定义

On NSObject Class defined

open class NSObject : NSObjectProtocol {
   public init()  
}

IQKeyboardReturnKeyHandler class

IQKeyboardReturnKeyHandler class

public override init() {
   super.init()    //Error here
}

public init(controller : UIViewController) {
   super.init()    //Error here
   addResponderFromView(controller.view)
}

有关如何更正此问题的任何建议吗?

Any suggestions on how to correct this?

推荐答案

我假设你的类IQKeyboardReturnKeyHandler你有这样的声明:

I assume that in your class IQKeyboardReturnKeyHandler you have such declaration:

weak var delegate: YourDelegateProtocol

既然它既不是可选的也不是隐式展开的你ave在之前初始化调用超类的初始值设定项。

Since it's neither an optional nor an implicitly unwrapped you have to initialize it before calling the initializer of the superclass.

但是,使用委托模式,最好将您的委托属性声明为可选:

However, using the delegate pattern, it's better to declare your delegate property as an optional:

weak var delegate: YourDelegateProtocol?

在这种情况下,没有必要在调用超类的初始值设定项之前设置委托,所以你的代码将如下所示:

In this case it isn't necessary to set the delegate before calling the initializer of the superclass, so your code will look like this:

weak var delegate: YourDelegateProtocol?

public override init() {
    super.init()
    self.delegate = nil
}

public init(controller : UIViewController) {
    super.init()
    self.delegate = controller
    addResponderFromView(controller.view)
}

重要!使用委托模式时,始终将您的委托属性声明为避免参考周期

Important! When using the delegate pattern, always declare your delegate property as weak to avoid reference cycles.

这篇关于升级到Swift 3后,属性'self.delegate'未在super.init调用时初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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