Checking if code is running in Interface Builder
Asked Answered
A

1

17

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!

Ashmead answered 10/6, 2015 at 0:15 Comment(2)
Don't know why this question was voted down. I think it's a great question. (Of course I have no useful answer...)Knapsack
I agree. This would be a very useful feature. (Voted)Whoso
C
24

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

Chlorinate answered 10/6, 2015 at 1:43 Comment(4)
Thanks! This was exactly what I was looking for and for some reason no amount of googling turned up that macro. Very useful!Ashmead
10/10 this is exactly what I needed!Musing
Wow, this is going to change my whole life dealing with IB crashes! Thank you x 1000!Farver
Thank you @EwanMellor, this made my day!!Calculated

© 2022 - 2024 — McMap. All rights reserved.