Checking if code is running in Interface Builder

2.4k views Asked by At

I have custom IBDesignable classes in swift that do some things that cause crashes in Interface Builder. Is there a flag I can check to see if I'm running in interface builder or not? Ideally it would be a precompiler directive (#ifdef COMPILED_FOR_INTERFACE_BUILDER or something like that). I thought maybe I could set a flag myself in prepareForInterfaceBuilder() but that seems messy and doesn't seem to work reliably as it looks class properties are set before that function is called and this can trigger code that causes problems. Seems like there must be a better way. Thanks!

1

There are 1 answers

4
Ewan Mellor On BEST ANSWER

From https://developer.apple.com/library/ios/recipes/xcode_help-IB_objects_media/Chapters/CreatingaLiveViewofaCustomObject.html:

You can use the preprocessor macro TARGET_INTERFACE_BUILDER to specify code for inclusion with or exclusion from your custom view class."