IBDesignable Errors When Adding to Tests Target IBDesignable Errors When Adding to Tests Target ios ios

IBDesignable Errors When Adding to Tests Target


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

STEP 1

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 }    }}

STEP 2

Import your application module from your "Tests" module.

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

import UIKitimport XCTestimport MyGreatAppclass 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.

STEP 3 (for Swift)

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

Interface Builder select main target module


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

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

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

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

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.

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.


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!.

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:

enter image description here

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