1 minute read

In a situation where our @IBDesignable is too complex to complete in under 200ms, we need a way to still get its benefits without the complex errors that it throws. You may argue that “well my code still builds just fine”. Sure, but now you have a bunch of errors and when you get one, you may have trouble debugging. Here is an easy solution.

In my example, I was generating a thumbnail from a video and overlaying that on top of the player as a preview image of the video. In my interface builder, upon further examination, this code was taking too long to execute and giving me the following errors.

Silvrback blog image

It’s important to note that none of those errors stopped me from building the project. But it definitely made it difficult to see when I had new errors popping up. The solution was simple. Have certain code run in the app and have different code run in the Interface Builder (IB). Here it is below.

  @IBInspectable var videoTime: Int32 = 0 {
    didSet {
      #if !TARGET_INTERFACE_BUILDER
        // this code will run in the app itself
        thumbnailOfVideo(time: videoTime)
        #else
        // this code will execute only in IB
      #endif

    }
  }