xcode 添加到测试目标时的 IBDesignable 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26030951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:41:33  来源:igfitidea点击:

IBDesignable Errors When Adding to Tests Target

iosswiftxcodexctestibdesignable

提问by Adolfo

I have a simple UIButtonsubclass that implements IBDesignablewith an IBInspectablevar:

我有一个使用varUIButton实现的简单子类:IBDesignableIBInspectable

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

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 Teststarget, it stops rendering live and I get the following errors:

我没有在框架内使用它,它在 Interface Builder 中按预期工作,但是,一旦我将此子类添加到我的Tests目标中,它就会停止实时渲染,并且出现以下错误:

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

If I remove IBDesignableand the IBInspectablevars, the errors go away - unfortunately so does the live rendering in Interface Builder.

如果我删除IBDesignableIBInspectable增值经销商,错误消失-可惜也是如此在Interface Builder中实时呈现。

How do I test against an IBDesignableclass without these errors?

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

采纳答案by rintaro

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

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

STEP 1

第1步

Mark your class and properties as public.

将您的类和属性标记为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 }
    }
}

STEP 2

第2步

Import your application module from your "Tests" module.

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

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

例如,假设您的应用程序名为MyGreatApp,在您的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")
    }
}

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

您不需要将 'Button.swift' 添加到您的“测试”目标。

STEP 3(for Swift)

第 3步(适用于 Swift)

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

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

Interface Builder select main target module

Interface Builder 选择主要目标模块

回答by Gerd Castan

Your question describes exactly the circumstances I experienced. The error is also visible in the attribute inspector under your own attributes.

你的问题准确地描述了我所经历的情况。该错误在您自己的属性下的属性检查器中也可见。

This is the workaround that worked for me:

这是对我有用的解决方法:

Step 1remove all @IBDesignable and @IBInspectable from your source code

步骤 1从源代码中删除所有 @IBDesignable 和 @IBInspectable

Step 2Go back to the interface builder. The error is still there.

步骤 2返回界面构建器。错误仍然存​​在。

Step 3Restart XCode, rebuild your project. Errors should be vanished.

步骤 3重启 XCode,重建你的项目。错误应该消失。

Step 4add all @IBDesignable and @IBInspectable again to your source code

步骤 4再次将所有 @IBDesignable 和 @IBInspectable 添加到您的源代码中

After this steps I was able to go on with my project without any problems.

在这些步骤之后,我能够继续我的项目而没有任何问题。

My theory why this works is that the interface builder caches some stuff that is not deleted (and rebuilt later) when you do a Project -> Clean.

我的理论为什么这样做是因为界面构建器会缓存一些在您执行 Project -> Clean 时不会删除(并在稍后重建)的内容。

The other answer (Importing your main module into your test module) is a good idea and solved some nasty problems for me in the past but not this one.

另一个答案(将您的主模块导入您的测试模块)是一个好主意,过去为我解决了一些棘手的问题,但没有解决这个问题。

回答by Bart?omiej Semańczyk

The solution is very simple. You need to remove your test target, and create it once again from the start. When I had moved the project to Xcode 7, I got an warning that my test target is damaged. So I decided to set it up once again. IT WORKED!.

解决方法很简单。您需要删除您的测试目标,并从头开始重新创建它。当我将项目移至 Xcode 7 时,我收到一条警告,指出我的测试目标已损坏。所以我决定再次设置它。有效!.

Additionally you do not attach your storyboards and not even any class to your test target. Please do not do it like this:

此外,您不会将故事板甚至任何类附加到您的测试目标。请不要这样做:

enter image description here

在此处输入图片说明

Instead, do it this way:

相反,这样做:

enter image description here

在此处输入图片说明

So, simply remove any file from your test target (including classes with @IBDesignables), then If you need access to your classes within your test target just use @testable import MyApp:

因此,只需从测试目标中删除任何文件(包括带有 @IBDesignables 的类),然后如果您需要访问测试目标中的类,只需使用@testable import MyApp

enter image description here

在此处输入图片说明

It is working and every errors with IBDesignables will disappear. Enjoy:-)

它正在运行,IBDesignables 的所有错误都将消失。享受:-)

回答by George WS

What is causing this?

这是什么原因造成的?

This error appears when two things happen:

发生以下两种情况时会出现此错误:

  1. You include any file containing @IBDesignableor @IBInspectablein your test target.
  2. You open your .storyboard or .xib file (containing your designable/inspectable views).
  1. 您包括任何包含@IBDesignable@IBInspectable在您的测试目标中的文件
  2. 您打开 .storyboard 或 .xib 文件(包含可设计/可检查的视图)。

How can I prevent this from happening?

我怎样才能防止这种情况发生?

Either:

任何一个:

  1. Don't include files containing @IBDesignableor @IBInspectablein your test target. (If you're just testing a few isolated model files, they can remain members of your test target.)
  2. Remove all of your app's files from your test target (as illustrated in Bart?omiej's answer), and instead import your app's module into your test file(e.g. @testable import MyAppsee here for more details.) This is the best practice, allowing you to continue testing your designable/inspectable views as well as any model code in your app.
  1. 不要包含包含@IBDesignable或包含@IBInspectable在测试目标中的文件。(如果您只是测试几个孤立的模型文件,它们可以保留为您的测试目标的成员。)
  2. 从测试目标中删除所有应用程序文件(如Bart?omiej 的回答所示),而是将应用程序的模块导入到测试文件中(例如@testable import MyApp-请参阅此处了解更多详细信息。)这是最佳实践,允许您继续测试您的可设计/可检查视图以及应用程序中的任何模型代码。

How can I solve it once it's already happened?

一旦它已经发生,我该如何解决?

The pesky thing with this error is that it doesn't automatically go away once the problem has been solved, potentially because IB may be caching something as Gerd suggests in his answer.

此错误令人讨厌的事情是,一旦问题得到解决,它就不会自动消失,这可能是因为 IB 可能正在缓存Gerd 在他的回答中建议的内容

What I've found is that once you've actually performed either of the two preventative measures I listed above, simply restarting Xcode (without modifying any code) should make the error go away.

我发现,一旦您实际执行了我上面列出的两种预防措施中的任何一种,只需重新启动 Xcode(不修改任何代码)即可使错误消失。

回答by CZ54

I finally found the solution. Juste add "-framework XCTest" to your test target under Build Settings / other linker flags.

我终于找到了解决方案。只需在 Build Settings / other linker flags 下将“-framework XCTest”添加到您的测试目标。

回答by Brian Croom

This issue can also be caused by including any categories or extensions on the view class in your test target.

此问题也可能是由于在测试目标中的视图类中包含任何类别或扩展而引起的。