Painted levels: being fostered fostered fostered

Xcode static Analysis “Analyze” “potential leak” by Xs·H review: QiShare team


In the process of iOS project development, static analysis, Breakpoint and Console are commonly used for code debugging. This article introduces a common Xcode debugging method called “static analysis.”

A list,

The static analysis function of Xcode is to analyze the context semantics, syntax, and memory of the code when the program is not running. Potential Localizability issues, Logic errors, Memery errors, Dead store, and syntax problems can be detected Foundation/Objective – C), etc. Function entry in Xcode menu bar -> Product -> Analyze (Command+Shift+B). The illustration is as follows.

Second, text localization

When project Target -> Build Settings -> Static Analyzer Missing Localizability is set to YES, Using the Analyze function, you can see that user-facing text should use String Macro prompts you with a text localization question. The illustration is as follows:

As prompted, modify the text with NSLocalizedString(<#key#>, <#comment#>), as shown below:

Alternatively, it is ok to set Missing Localizability to NO. The illustration is as follows:

Third, logical problems

Use Analyze to identify potential logical problems in your code. For example, using the NSNumber object directly as a condition, you can get a warning: Pointer value of type ‘NSNumber *’ to a primitive Boolean value; Instead, either compare the pointer to nil or call-BoolValue. The illustration is as follows:

As prompted, we have two modification methods, as shown below:

Although these two modification methods can solve the problem, but not exactly the same, pay attention to the differences, choose the required use.

Four, memory problems

Although ARC helps developers manage most of the memory issues. But C series code like CGImageRef also requires the developer to manage memory. These memory problems are difficult to detect during compilation, but use Analyze to give an indication. The illustration is as follows:

There are two memory-related hints in the image above that developers can follow to modify accordingly. The illustration is as follows:

5. Data issues

During the coding process, some data problems can be well indicated by using Analyze. Here’s an example:

The string in the figure above is suggested never to be used after initialization. Because the assignment in the second part reappropriates memory for string. At this point, the developer removes the first initialization method. The illustration is as follows:

Sixth, grammar problems

Use Analyze to detect syntax problems that occur when rewriting a class’s init method by hand. The illustration is as follows:

If = = = = = = = = = = = Modified as follows:

conclusion

The above sections are just examples of the Analyze function, which can actually detect more problems. In addition, Analyze has its own limitations. The analysis results only assist the developer in finding problems. Truly optimizing the code requires more technical experience.


Recommended articles:

IOS Message forwarding iOS custom drag-and-drop control: QiDragView iOS custom card control: QiCardView iOS Wireshark capture iOS Charles capture QiCardView Weekly