添加到测试目标时的IBDesignable错误 [英] IBDesignable Errors When Adding to Tests Target

查看:169
本文介绍了添加到测试目标时的IBDesignable错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 UIButton 子类,用 IBInspectable IBDesignable c $ c> var:

I have a simple UIButton subclass that implements IBDesignable with an IBInspectable var:

@IBDesignable class Button: UIButton {
    @IBInspectable var borderColor: UIColor = UIColor.whiteColor() {
        didSet { layer.borderColor = borderColor.CGColor }
    }
}

我没有在框架中使用它,它在Interface Builder中按预期工作,但是,一旦我将这个子类添加到我的 Tests 目标,它停止渲染实时,我得到以下错误:

I am not using this within a framework and it is working in Interface Builder as intended, however, once I add this subclass to my Tests target, it stops rendering live and I get the following errors:

Main.storyboard: error: IB Designables: Failed to update auto layout status: dlopen(TestTests.xctest, 1): Library not loaded: @rpath/XCTest.framework/XCTest
Referenced from: TestTests.xctest
Reason: image not found

Main.storyboard: error: IB Designables: Failed to render instance of Button: dlopen(TestTests.xctest, 1): Library not loaded: @rpath/XCTest.framework/XCTest
Referenced from: TestTests.xctest
Reason: image not found

如果我删除 IBDesignable IBInspectable vars,错误就会消失 - 不幸的是,实时渲染也是如此Interface Builder。

If I remove IBDesignable and the IBInspectable vars, the errors go away - unfortunately so does the live rendering in Interface Builder.

如何在没有这些错误的情况下测试 IBDesignable 类?

How do I test against an IBDesignable class without these errors?

推荐答案

起初,我认为这是Xcode中的一种错误。以下是我找到的解决方法:

At first, I thought this was a kind of bug in Xcode. Following is the workaround I found:

第1步

标记您的课程和属性为 public

Mark your class and properties as public.

@IBDesignable public class Button: UIButton {
    @IBInspectable public var borderColor: UIColor = UIColor.whiteColor() {
        didSet { layer.borderColor = borderColor.CGColor }
    }

    @IBInspectable public var borderWidth:CGFloat = 0.0 {
        didSet { layer.borderWidth = borderWidth }
    }
}

第2步

从测试模块导入应用程序模块。

Import your application module from your "Tests" module.

例如,假设您的应用程序名为 MyGreatApp ,在 MyGreatAppTests / MyGreatAppTests中.swift

For example, assuming that your application is named MyGreatApp, in your MyGreatAppTests/MyGreatAppTests.swift:

import UIKit
import XCTest
import MyGreatApp

class MyGreatAppTests: XCTestCase {

    func testExample() {
        let btn = Button()
        btn.borderColor = UIColor.redColor()
        XCTAssertEqual(UIColor(CGColor:btn.layer.borderColor), UIColor.redColor(), "borderColor")
    }
}

您无需在测试目标中添加Button.swift。

You don't need to add 'Button.swift' to your "Tests" target.

第3步(适用于Swift)

STEP 3 (for Swift)

在您的故事板中,为任何自定义类明确选择模块MyGreatApp,而不是让Xcode使用当前模块。

In your storyboard explicitly select the module MyGreatApp for any custom classes instead of letting Xcode use the current module.

这篇关于添加到测试目标时的IBDesignable错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆