Welcome to our public account, we will share original articles every week. We are mainly positioned in the field of mobile development, sharing mobile development technologies, including iOS, Android, applet, mobile front-end, React Native, weex, etc.
"Damn it, how do I check this question? I've tried dozens of times, but I can't reproduce it?", "I'll go, this question makes me unbelievable"... I often hear this kind of voice, so today I'm going to make it easy for everyone Introduce Xcode
several super easy-to-use diagnostic tools that come with them to improve your work efficiency. (This article will not introduce detailed usage methods, please Google or Baidu by yourself.)
Be kind to compilation warnings
Xcode
We have prepared a very rich compilation warning for us. Although these warnings will not lead to compilation, there must be no hate for no reason behind it. Treat Warnings as Errors
It's also a good idea to treat all warnings correctly and avoid some elusive behavior at runtime . If you are absolutely confident about some warnings, you can eliminate them by Build Settings
modifying the Warnings
options in , or by adding diagnostic ignore
to . With Xcode
the continuous update of , you should also Editor-Validate Settings
change the project configuration options to optimal through the project file's .
Don't Ignore Analyze
Xcode
Static detection tools have been provided for a long time for us to detect a range of hard-to-find problems, such as memory leaks, logic errors, unused variables, and libraries not included. For many projects, a full run Analyze
will take a long time, and perhaps because of this, this tool is being ignored by more and more developers. You can perform Analyze in real-time during the usual development and debugging process by modifying Build Settings
it .Analyze During 'Build'
runtime diagnostics
In addition to compilation warnings Analyze
, Apple also provides us with more dynamic debugging tools. They are scattered Xcode
in every corner of , and are closely related to our daily development.
Debug Gauges
When you App
start debugging, Xcode
the navigator
view will automatically switch to debug navigator
. Here, several key metrics are displayed by default to give you an overview of App
the operational status. Through them, you can get a general understanding of the operation of each thread, memory application, power usage, disk read and write, network operations, etc. If you care about more detailed diagnostic information, you can easily enter Instruments
to make the corresponding diagnosis.
###Process Debug
Xcode
Provides us with four kinds of process debugging:
-
View Process by Thread
: The call stack of the thread, this is the default and the most commonly used. Add a breakpoint to view all current thread calls. -
View Process by Queue
:View Process by Thread
Similar to , the current queue name will replace theThread
name, and the color should be richer. -
View UI Hierarchy
: You can switch to God's perspective to view the view hierarchy and view properties. -
View Memory Graph Hierarchy
: It can easily detect memory leaks such as circular references, which is very practical.
Enable Diagnostics
In addition Xcode
to providing us with Guard Malloc
, Zombie Objects
etc. in the early days, Sanitizers
and Main Thread Checker
further improve the detection tools in the development process.
- Address Sanitizer
For the MRC
environment , OC
it will be triggered to operate on an object after it is released crash
; after entering the ARC
era , we care less and less about this issue. But when you use Core Foundation
, or C\C++
mix , this problem can behave unexpectedly. When a memory area is freed, there is no problem accessing it until the area is reallocated, which brings a lot of randomness. In addition, if you accidentally cross the bounds when using pointers to access memory, the consequences are unpredictable. For these problems, Xcode
is provided Address Sanitizer
to test more robustly.
- Thread Sanitizer
In the past, Data Race
it was very difficult to locate and troubleshoot problems, because the problems it brought were often unpredictable. Xcode
By recording information such as the time of each memory access, the detection is performed each time the memory is accessed. You can selectively enable this tool in the daily development and debugging process, but in unit testing, you should consider enabling it. If your use case coverage is high, it may be able to find most of the multi-threaded data competition for you. question.
- Undefined Behavior Santizer
Undefined text detection, such as dividing by 0, loading memory to its pointer, non-associating a null pointer, etc. It may crash
or crash
, depending on the specific situation and the compiler, but we should avoid this kind of thing from happening.
- Main Thread Checker
Detection is used in child threads AppKit
, UIKit
and others API
, this detection tool is enabled by default, and does not need to be recompiled after it is enabled. The principle is to automatically replace the API that can only be used in the main thread after the app is started, and throw an exception when calling such an API.
It is worth mentioning that since the implementation principle of Address Sanitizer
, orGuard Malloc
directly or indirectly affects the memory management of runtime objects, if these diagnostic tools are enabled, one will fail.Zombie Objects
Debug Gauges
Memory
Summarize
Solving warnings and diagnoses as soon as possible App
will greatly improve App
the stability after going online, and it can also improve our work efficiency, so what are you waiting for, act quickly.