简体   繁体   English

在'super.init'调用之前的方法调用'foo'中使用的'self'

[英]'self' used in method call 'foo' before 'super.init' call

I am using this open source project from github : https://github.com/barnaclejive/FaceTrigger 我正在使用来自github的开源项目: https : //github.com/barnaclejive/FaceTrigger

I am unable to solve this error in child class: 我无法解决子类中的此错误:

Error: 'self' used in method call 'onBoth' before 'super.init' call 错误:调用“ super.init”之前在方法调用“ onBoth”中使用“ self”

 class BrowDownEvaluator: BothEvaluator {

   func onBoth(delegate: FaceTriggerDelegate, newBoth: Bool) {
    delegate.onBrowDownDidChange?(browDown: newBoth)
    if newBoth {
        delegate.onBrowDown?()
    }
}

func onLeft(delegate: FaceTriggerDelegate, newLeft: Bool) {
}

func onRight(delegate: FaceTriggerDelegate, newRight: Bool) {
}

init(threshold: Float) {
    super.init(threshold: threshold, leftKey: .browDownLeft, rightKey: .browDownRight, onBoth: onBoth, onLeft: onLeft, onRight: onRight)
  }
}

Parent Class: 家长班:

  class BothEvaluator: FaceTriggerEvaluatorProtocol {

    private let threshold: Float
    private let leftKey: ARFaceAnchor.BlendShapeLocation
    private let rightKey: ARFaceAnchor.BlendShapeLocation
    private var onBoth: (FaceTriggerDelegate, Bool) -> Void
    private var onLeft: (FaceTriggerDelegate, Bool) -> Void
    private var onRight: (FaceTriggerDelegate, Bool) -> Void

    private var oldLeft  = false
    private var oldRight  = false
    private var oldBoth  = false

init(threshold: Float,
     leftKey: ARFaceAnchor.BlendShapeLocation ,
     rightKey: ARFaceAnchor.BlendShapeLocation ,
    onBoth: @escaping (FaceTriggerDelegate, Bool) -> Void,
    onLeft: @escaping (FaceTriggerDelegate, Bool) -> Void,
    onRight: @escaping (FaceTriggerDelegate, Bool) -> Void)
{
    self.threshold = threshold

    self.leftKey = leftKey
    self.rightKey = rightKey

    self.onBoth = onBoth
    self.onLeft = onLeft
    self.onRight = onRight
}

I understand that I have to initialize onBoth and rest of the methods here But how do you initialize the method? 我了解我必须在这里初始化onBoth和其余方法,但是如何初始化方法? I am still learning Swift. 我还在学习Swift。

You should not set instance methods into instance properties even where referencing self.methodName is allowed, which makes reference cycles. 即使在允许引用self.methodName情况下,也不应将实例方法设置为实例属性,这会导致引用循环。

A simple workaround is something like this: 一个简单的解决方法是这样的:

class BrowDownEvaluator: BothEvaluator {
    static func onBoth(delegate: FaceTriggerDelegate, newBoth: Bool) {
        delegate.onBrowDownDidChange?(browDown: newBoth)
        if newBoth {
            delegate.onBrowDown?()
        }
    }

    static func onLeft(delegate: FaceTriggerDelegate, newLeft: Bool) {
    }

    static func onRight(delegate: FaceTriggerDelegate, newRight: Bool) {
    }

    init(threshold: Float) {
        super.init(threshold: threshold, leftKey: .browDownLeft, rightKey: .browDownRight,
                   onBoth: BrowDownEvaluator.onBoth,
                   onLeft: BrowDownEvaluator.onLeft,
                   onRight: BrowDownEvaluator.onRight)
    }
}

If you want to access self as an instance of BrowDownEvaluator , things get a little more complicated. 如果您想作为BrowDownEvaluator的实例访问self ,事情会变得更加复杂。

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

相关问题 'self' 在 super.init 调用之前使用 - 'self' used before super.init call 在super.init调用之前使用“ self”,并且在super.init调用中未初始化属性 - 'self' used before super.init call and property not initialized at super.init call RealmSwift初始值设定项:在super.init调用之前自行使用 - RealmSwift initializer: self used before super.init call Swift 1.2:在super.init调用之前使用的'self' - Swift 1.2: 'self' used before super.init call Swift 4.1:“在“ super.init”调用之前,自用方法调用“ defaultConfiguration”” - Swift 4.1 : “self used method call 'defaultConfiguration' before 'super.init' call” '在super.init初始化self之前在方法调用中使用self',不能通过方法调用初始化属性 - 'Use of self in method call before super.init initializes self', can't init properties through a method call 继承和构造函数。 在super.init之前自用 - Inheritance and constructors. Self used before super.init 从super.init调用可以在'catch'块内使用'self' - 'self' used inside 'catch' block reachable from super.init call Swift:属性“ self.tableView”未在super.init调用中初始化 - Swift: Property 'self.tableView' not initialized at super.init call 属性 'self.readCount' 未在 super.init 调用时初始化 - Property 'self.readCount' not initialized at super.init call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM