[英]What is the best practice to write the debugging and production code at the same time?
I develop iOS apps and use swift and Xcode now. 我现在开发iOS应用程序并使用swift和Xcode。 I often write like this when I use a test code for debug.
当我使用测试代码进行调试时,我经常这样写。
//test
label.backgroundColor = UIColor.red
//set bicolor
label.backgroundColor = UIColor.clear
If it's test mode (before release app), I want to set backgroundColor in red. 如果它是测试模式(在发布应用程序之前),我想将backgroundColor设置为红色。 When I submit the app to store, the test code that set backgroundColor to red will be comment out and The backgroundColor will be set clear.
当我将应用程序提交到商店时,将backgroundColor设置为红色的测试代码将被注释掉,并且backgroundColor将被设置为清除。
What is the best practice to write the debugging and production code at the same time?. 同时编写调试和生产代码的最佳做法是什么? Is the abouve is good practice or not?.
是不是好的做法?
What you can do is use if development 你可以做的就是开发时使用
label.backgroundColor = UIColor.clear
#if DEVELOPMENT
label.backgroundColor = UIColor.red
#endif
or 要么
label.backgroundColor = UIColor.red
#if RELEASE
label.background = UIColor.clear
#endif
There are 3 #pragma_mark available in swift same as Objective-C: 与Objective-C相同,swift中有3个#pragma_mark :
// MARK: - write your text here
// MARK: -
write your text here
// TODO: - write your text here
// TODO: -
write your text here
// FIXME: - write your text here
// FIXME: -
write your text here
You can use -
to add separator line. 您可以使用
-
添加分隔线。
Using Swift flags DEBUG and RELEASE, you can separate the code for DEBUG and RELEASE 使用Swift标志DEBUG和RELEASE,您可以分离DEBUG和RELEASE的代码
https://kitefaster.com/2016/01/23/how-to-specify-debug-and-release-flags-in-xcode-with-swift/ https://kitefaster.com/2016/01/23/how-to-specify-debug-and-release-flags-in-xcode-with-swift/
In Swift you can use the following code: 在Swift中,您可以使用以下代码:
#if RELEASE
// release only code
#else
// debug only code
#endif
I normally write following method in Android 我通常在Android中编写以下方法
If you want some line of code for debugging and production, you can do the following, 如果您需要一些代码用于调试和生产,您可以执行以下操作,
Create a global boolean value like below in GlobalVariableClass, 在GlobalVariableClass中创建如下所示的全局布尔值,
public boolean debug=true
Then you can write code like, 然后你可以写代码,
if(GlobalVariableClass.debug){
//debug code
}else{
//Production code
}
It also helpfull for log data as , 它还有助于日志数据,因为,
If(GlobalVariableClass.debug){
Log.e("TAG","log message");
}
So for the production time, you need to change the debug
in GlobalVariableClass
to false
因此,对于生产时间,您需要将
GlobalVariableClass
的debug
更改为false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.