IOS---different nslog printing (location printing)

Source: Internet
Author: User

In iOS development process, debugging is very important process, but in addition to various breakpoint debugging (ordinary breakpoint, conditional breakpoint, global breakpoint), it seems that nslog is our most common method of debugging, of course, is also the simplest simple method of seeking debug.

In the project, we often use the NSLog statement of the following one:

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

NSLog (@ "Print shaping:%i", number);//or%li; %ld; %d

NSLog (@ "Print character:%c", c);

NSLog (@ "Print single floating point:%f", f);

NSLog (@ "Print precision floating point number:%.2f", f);

NSLog (@ "bool--b-->%@", [email protected] "yes": @ "NO");//Print Boolean type

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

Let's try it: Create an empty array-->dataarray, but print the third element of the array. Printing results such as:

From what we can see, a standard array is out of bounds, but we see that the debug output just says another array out of error, but does not indicate which array, where the array is located ... Imagine: if we have several arrays in a viewcontroller, then the array appears out of bounds. It will be difficult to find out which array is debug.   So the question arises, how can we use NSLog to determine the location? The following is the beginning of the topic : different NSLog printing

To use a different nslog, first understand the following: The macros provided by the preprocessor in the C/c++/objective-c language

  

* __func__%s Current function signature

* __line__%d the current number of lines in the source code file----> macro is replaced with the current line number at precompilation

* __file__%s Current source file full path--macros will be replaced with the current source file name at precompiled time

* __pretty_function__%s is like __func__, but contains stealth type information in C + + code. ----> macros are replaced with the current function name at precompilation

As for log, the current function of printing is nothing more than _cmd, __func__, __function__, 4.

1. Print the current function name and the number of rows in the file where the current code is located

NSLog (@ "%s,%d", __function__, __line__);//The function name directly to debug, and the number of rows in the file where the current code resides.

Enter the following code in -(void) viewdidload ...

The printing results are as follows:

2. Print the current function name, nsstringfromselector the string for the method represented by the selector for the parameter

NSLog (@ "%@", Nsstringfromselector (_cmd));

The printing results are as follows:

  3. Print the current source code file full path

NSLog (@ "%s", __file__);

 The printing results are as follows:

4. Using __pretty_function__ ,described above: like __func__, but contains stealth type information in C + + code.

NSLog (@ "%s", __pretty_function__);

 Printing results:

In Xcode:

_cmd will return a Sel object, and all that is left is the definition of C/C + + compiler, so it will return a string with the same result, and it will probably have a small difference between the compilers.

Obviously behind the __FUNC__ series is better than _cmd, relative to the Objective-c type of method calls, he will not only display the method name, but also display the type, with __line__, you can precisely locate the location of the log in the code.

Here are the articles I read when I studied, and share them with you. Know how to be thankful and respect the fruits of their labor:

"Ios/cocoa: Using more code-positioned log"

iOS all kinds of debugging skills luxury Package

Well, the above is what I would like to introduce NSLog help in the development of positioning printing knowledge. I'm in touch with things I like and use. There are a lot of things that we don't know about iOS, and we don't even know it. Step-by-step, groping---learning---practice---- Every step is very hard, hold on, go forward will be a different scenery.

IOS---different nslog printing (location printing)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.