【问题标题】:'self' used in method call 'foo' before 'super.init' call在“super.init”调用之前在方法调用“foo”中使用“self”
【发布时间】:2018-10-02 08:30:03
【问题描述】:

我正在使用来自 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。

【问题讨论】:

    标签: swift


    【解决方案1】:

    即使允许引用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,事情会变得有点复杂。

    【讨论】:

    • 这并不能解决问题。我现在收到此错误:无法将类型“(BrowDownEvaluator)->(FaceTriggerDelegate,Bool)->()”的值转换为预期的参数类型“(FaceTriggerDelegate,Bool)-> Void”
    • @ShikhaShah,你是不是在方法之上缺少statics?
    • 原谅我的迅捷技能。无法通过错误消息弄清楚。这样就解决了问题。
    • @ShikhaShah,这告诉我在显示有细微变化的代码时应该注意一些事情。无论如何,很高兴听到这个消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    相关资源
    最近更新 更多