IBDesignable Errors When Adding to Tests Target

前端 未结 6 1511
青春惊慌失措
青春惊慌失措 2020-12-14 08:12

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

@IBDesignable class Button:          


        
6条回答
  •  鱼传尺愫
    2020-12-14 08:46

    What is causing this?

    This error appears when two things happen:

    1. You include any file containing @IBDesignable or @IBInspectable in your test target.
    2. You open your .storyboard or .xib file (containing your designable/inspectable views).

    How can I prevent this from happening?

    Either:

    1. Don’t include files containing @IBDesignable or @IBInspectable in 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 MyApp—see 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.

    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.

    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.

提交回复
热议问题