首页 > 解决方案 > 覆盖测试目标中的协议默认实现

问题描述

我的主要目标中有一个协议:

protocol Animatable: UIView {
    static var shouldAnimate: Bool { get } 
}

和我的主要目标中的默认实现:

extension Animatable {
    static var shouldAnimate: Bool { return true } 
}

和符合此协议但没有实现的对象.shouldAnimate

class MyView: Animatable { }

在我的测试目标中,我想覆盖默认实现,所以我写道:

extension MyView {
    static var shouldAnimate: Bool { return false } 
}

在我主要目标的代码中的其他地方我写了print(MyView.shouldAnimate)end 我希望看到输出:

  1. 如果我只运行主要目标true
  2. 如果我运行我的测试false

但是当在这两种情况下我看到true并且默认实现总是从这个协议中触发?为什么?运行测试目标时不应该用类实现覆盖吗?

标签: iosswiftmacos

解决方案


推荐阅读