In iOS development, debugging is an important process, and it seems that NSLog is the most common way to debug (normal breakpoints, conditional breakpoints, global breakpoints), and of course, the simplest way to find debugging.

In projects, we often use NSLog statements like the following:

NSLog(@” Print string: %@”,name);

NSLog(@” print integer: % I “,number); // or %li; %ld ; %d

NSLog(@” print character: %c”,c);

NSLog(@” print single float: %f”,f);

NSLog(@” Print accuracy float: %.2f”,f);

NSLog(@”BOOL–b–>%@”,isYES? @”YES”:@”NO”); // Prints a Boolean type

But this is not the result we want, because the printed result, sometimes we do not know the specific location of the print, or even specific information. For example, we often encounter arrays that are out of bounds, network request data that is empty, and so on.

Let’s try: Create an empty array –>dataArray and print the third element of the array. The print result is as follows:

From the above we can see that a standard array is out of bounds, but we can see that the debug output just says that another array is out of bounds, but it does not indicate which array is out of bounds, or where the array is. Imagine: If we have several arrays in a ViewController, the array is out of bounds. It will be hard to figure out which array is debugged. So the question is, how can YOU use NSLog to determine the location? Let’s get started on the topic: different NSLog printing

To use a different NSlog, first understand the following: macros provided by the preprocessor in C/C++/ Objective-C

C/C++/Objective-C preprocessor macro for logging output.

Macro Format Specifier Description
__func__ %s Before the current function
__LINE__ %d The line number in the source file
__FILE__ %s Source file complete path
__PRETTY_FUNCTION__ %s Similar to __func__, but with more information in C++ code.

An expression used for logging output in Objective-C

Expression Format Specifier Description
NSStringFromSelector(_cmd) % @ The name of the current selector
NSStringFromClass([self class]) % @ Name of the current object class
[[NSString stringWithUTF8String:__FILE__] lastPathComponent] % @ The name of the source file

[NSThread callStackSymbols]

An array of graduated strings for current stack information. Used only for debugging and not to be shown to end users or used as any logic in code.

* __func__%s The signature of the current function

* __LINE__ %d Number of current lines in the source file —-> macros are replaced with the current line number when precompiled

* __FILE__ %s Full path to the current source file –> macros are replaced with the current source file name when precompiled

* __PRETTY_FUNCTION__ %s is like __func__, but contains invisible type information in C++ code. —-> Macros are replaced with the current function name when precompiled

For Log, the current function is nothing more than _cmd, __func__, __FUNCTION__, PRETTY_FUNCTION.

1. Print the current function name and the number of lines in the file where the current code resides

NSLog(@”%s, %d”, __FUNCTION__, __LINE__); // Directly locate the function name of debug and the number of lines in the file where the current code resides.

Enter the following code in – (void)viewDidLoad…

The print result is as follows:

2. Print the current function name, NSStringFromSelector gets the string of the method represented by the argument’s selector

     NSLog(@”%@”, NSStringFromSelector(_cmd));

The print result is as follows:

  3. Print the full path of the current source code file

NSLog(@”%s”, __FILE__);

 The print result is as follows:

     

4. Use __PRETTY_FUNCTION__

Like __func__, but contains invisible type information in C++ code.

NSLog(@”%s”, __PRETTY_FUNCTION__);

 Print result:

In Xcode:

_cmd returns a SEL object, and the rest is all definitions from the C/C++ compiler, so it all returns a C string, and the results are pretty much the same, maybe a little different from compiler to compiler.

The __func__ series is obviously better than _cmd. Compared to method calls in Objective-C, it displays not only the method name, but also the type. With __LINE__, it can precisely locate the Log position in the code.

The following are the articles I read during my study and share them with you. Be grateful and respect the fruits of their labor:

IOS /Cocoa: Using more Locatable Logs for code

IOS Debugging Skills Deluxe Package

Ok, so that’s what I want to introduce to NSLog to help position printing in development. It’s all about me being exposed to things THAT I like and then use. There are many, many things about iOS that we don’t understand or even know about. Step by step, explore -> learn -> practice -> master. Every step is very hard, stick to it, go forward will be different scenery.