簡體   English   中英

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

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

我正在使用來自github的開源項目: https : //github.com/barnaclejive/FaceTrigger

我無法解決子類中的此錯誤:

錯誤:調用“ 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)
  }
}

家長班:

  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
}

我了解我必須在這里初始化onBoth和其余方法,但是如何初始化方法? 我還在學習Swift。

即使在允許引用self.methodName情況下,也不應將實例方法設置為實例屬性,這會導致引用循環。

一個簡單的解決方法是這樣的:

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

如果您想作為BrowDownEvaluator的實例訪問self ,事情會變得更加復雜。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM