iOS: optional code fragments for debug builds

微笑、不失礼 提交于 2019-12-12 07:10:12

问题


for my App I'd like to have a debug view that I want to have only in debug-builds and not in release builds. I don't want to change my code though. Thats why i am wondering if I can check some compiler flag if this is a release build and exclude some code that i only want to have for debug builds.


回答1:


In your projects build settings, look for the preprocessor defines section, in there you can define a variable in your debug build only, such as DEBUG=1, and then use this in your code:

#if DEBUG
    NSLog(@"This will only print in debug!");
#endif

Just make sure in your release configuration, that same define is set to 0 in the same location in your build settings




回答2:


Check your projects build settings for debug to ensure that 'DEBUG' is being set - Apple gives you this for free - do this by selecting the project and clicking on the build settings tab. Search for 'DEBUG' and look to see if indeed DEBUG is being set.

then conditionally code for DEBUG in your source files

#ifdef DEBUG

// Something to log your data here or even add a whole subview to see it on the device

#else

// 

#endif


来源:https://stackoverflow.com/questions/9081334/ios-optional-code-fragments-for-debug-builds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!