swift - 在 'foo' 调用之前的方法调用 'super.init' 中使用了“self”

标签 swift

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

如果您想将 self 作为 BrowDownEvaluator 的实例进行访问,事情会变得有点复杂。

关于swift - 在 'foo' 调用之前的方法调用 'super.init' 中使用了“self”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52600389/

相关文章:

特定类的 Swift 协议(protocol)?

swift - CALayer 和 Uiview

swift - 使用 UIPopoverBackgroundView 自定义的 Popover 中的边框问题

ios - 如何为某些 View Controller 锁定 iPhone 的方向 - Swift?

ios - 更改 UIAlertcontroller 背景颜色

swift - 使用 Swift Parse Server 和 AWS SES 请求重置电子邮件密码

swift - NSAttributedString 绘制位置在 10.13 和 10.14+ 上不同

swift - 使用 NSURLSession 时 GET 成功,但使用 AFHTTPSessionManager 时 GET 失败

swift - 向 ColorSprites 添加点击功能

ios - 如何使用 Magical Record 设置自动迁移核心数据堆栈?