Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if code is running in Interface Builder

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!

like image 209
Michael John Ilardi Avatar asked Jun 10 '15 00:06

Michael John Ilardi


1 Answers

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

like image 119
Ewan Mellor Avatar answered Oct 02 '22 00:10

Ewan Mellor