Swift

时间:2018-01-05 14:47:38

标签: ios swift uiview xib xctest

如何将一个.xib的类设置为UIView类型的自定义类?

我想对此进行单元测试,并且我认为我需要从nib实例化视图(使用IBOutlets完成),然后测试类似:tempSearchCardView.titleLabel.text == "Hello World"

我已尝试tempSearchCardView = Bundle.main.loadNibNamed("SearchCardView", owner: nil, options: nil)![0] as? SearchCardView,但tempSearchCardView = nil

...

这是我的IB情况:

enter image description here

...

这是我的.swift文件:

enter image description here

1 个答案:

答案 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按预期工作!!!