swift - 如何断言结构符合协议?
问题描述
我有一个测试用例断言一个类符合协议。
let sut = SomeClass()
..........
func test_some_class_conform_to_protocol() {
XCTAssertTrue((sut as Any) is OverlayManagerType)
}
我正在尝试使用struct
符合协议的 a 来实现相同的测试,但是测试不断失败。
有可能实现这一目标吗?
编辑
我已经添加了我的结构。我正在遵循 TDD 方法,因此目前还没有实现。
protocol CountManagerType {
}
struct CountManager: CountManagerType {
}
我的测试是
func test_count_manager_conform_to_protocol() {
XCTAssertTrue((sut as Any) is CountManagerType)
}
解决方案
在以下示例中,您的代码对我来说很好
protocol CountManagerType {
}
struct CountManager1: CountManagerType {
}
struct CountManager2 {
}
let c1 = CountManager1()
print(((c1 as Any) is CountManagerType)) // true
let c2 = CountManager2()
print(((c2 as Any) is CountManagerType)) // false
推荐阅读
- php - 字符串到 stdClass php
- google-translate - 翻译 API - 与 Web 服务不同的结果
- php - 如何在此联系表中使用 OOP PHP?
- maven - Maven 多模块依赖
- google-apps - “与 Google 集成”按钮消失了
- sql-server - SQL Server 中的动态 SQL:通过 sp_sqlexec @string VS 使用纯 SQL 查询执行字符串 - 为什么存储的字符串不起作用?
- powerbi - Power BI Desktop 的一个切片器影响多个表
- java - 从 EJBContext 检索 UserTransaction 时出错,抛出 IllegalStateException
- xamarin - 将 Xamarin XAML 绑定到属性背后的代码 (ListView)
- php - 如果我要在数据库中存储两条记录,则需要测量表单元