如何将一个.xib的类设置为UIView类型的自定义类?
我想对此进行单元测试,并且我认为我需要从nib实例化视图(使用IBOutlets完成),然后测试类似:tempSearchCardView.titleLabel.text == "Hello World"
我已尝试tempSearchCardView = Bundle.main.loadNibNamed("SearchCardView", owner: nil, options: nil)![0] as? SearchCardView
,但tempSearchCardView = nil
...
这是我的IB情况:
...
这是我的.swift文件:
答案 0 :(得分:0)
我在这里找到了答案:Swift 2.0: Could not cast value MyApp.MyCustomClass to MyAppTests.MyCustomClass when using Set
我尝试了大多数我能找到的其他教程/建议。
上面的帖子说要从测试目标中删除文件,只需@testable import MyApp
...然后testSearchCardView = Bundle.main.loadNibNamed("SearchCardView", owner: self, options: nil)?.first as! SearchCardView
按预期工作!!!