简体   繁体   English

同时编写调试和生产代码的最佳做法是什么?

[英]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

  1. // MARK: - write your text here // MARK: - write your text here

  2. // TODO: - write your text here // TODO: - write your text here

  3. // 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 因此,对于生产时间,您需要将GlobalVariableClassdebug更改为false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中编写完整测试(以单元为单位)的最佳实践是什么? - What is the best practice to write full tests(Unit-Instrumented) in android? 在Android上同时将数据保存在内存和数据库中的最佳实践 - Best practice for keeping data in memory and database at same time on Android 用离子写代码的最佳方法是什么 - what is the best way to write the code in ionic 在定位android和ios时最大程度地重用代码的最佳实践是什么 - What is the best practice to maximize reuse of code when targeting android and ios 文件写入的最佳实践是什么 - What is the best practice for file writing 获得 Looper 的最佳做法是什么? - What is the best practice to get Looper? 使用CompositeDisposables的最佳实践是什么 - What is the best practice of using CompositeDisposables 什么是SQLite + RxJava的最佳实践? - What is the best practice SQLite + RxJava? 在同一个 Android 应用程序中使用多个 Firebase 项目的 Firebase 云消息传递的当前最佳实践是什么? - What is the current best practice for using Firebase Cloud Messaging with multiple Firebase projects in the same Android app? Android应用启动时间的最佳做法 - The best practice for app launch time duration Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM