@IBDesignable crashing agent

前端 未结 10 892
自闭症患者
自闭症患者 2020-11-29 02:59

When I write my own UIButton-extended class and make it @IBDesignable, I receive two errors in Interface Builder, namely:

  • Main.storyb
10条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 03:55

    For me, it was not using #if !TARGET_INTERFACE_BUILDER that got me. Basically I had some code that would result in accessing a path in my Bundle...

    Bundle.main.path(forResource: "Foo", ofType: "plist")
    

    The problem is that when running in IB (and not in your app), Bundle.main isn't your app...

    (lldb) po Bundle.main
    NSBundle  (loaded)
    

    So the answer to this is simply to review your @IBDesignable UIView code carefully and use #if !TARGET_INTERFACE_BUILDER for anything (in my case calls to CoreLocation for example) that doesn't make sense when running at design time.

    How I debugged and found this

    This is what you see when using the Editor -> Debug Selected View while selecting your @IBDesignable UIView in your storyboard:

    You'll then crash at the right location

    In my case, as you can see initXXXX was doing an assert, which was crashing at design time, because it was looking for a value in file in my Bundle — which is Xcode, at design time.

提交回复
热议问题