简体   繁体   中英

'self' used in method call 'foo' before 'super.init' call

I am using this open source project from 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

 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? I am still learning Swift.

You should not set instance methods into instance properties even where referencing self.methodName is allowed, which makes reference cycles.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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