Objective-c and Swift Custom print functions (Debug and release)

Source: Internet
Author: User

1.objective-c

In the process of developing with objective-c, the print function is constantly set up for debug. As we often use to test the implementation of a listening method:

1     NSLog (@ "%s", __func__); 2     NSLog (@ "%s", __function__);

It should be stated that both __func__ and __function__ are predefined symbols of C and represent exactly the same meaning, that is, a string that returns the class name + method name.

Note: 1. The returned string is a C-language string char * type, note that the placeholder is "%s".

2. Two predefined symbols are two underscores before and after, and be careful not to write one.

Printing results:

1 -One-all:05.769 roonensmartlifesecondphase[   47510:1409025]-[Jtsmartlifeviewcontroller buttondidpress:]2- -All:05.770 roonensmartlifesecondphase[47510 :1409025]-[jtsmartlifeviewcontroller buttondidpress:]

As you can see, exactly the same.

However, it is important to note that our program is divided into two modes: Debug and release. You can see in this location:

The meaning of the two patterns is clear from their naming: 1. Debug: Debug 2.Release: Publish. The difference is obvious, in debug mode, it is natural to not consider the use of resources to find bugs for the purpose, and release mode naturally to maximize performance optimization.

Then we look back to print this function: In the debug mode, printing is very useful, in the release mode, printing is useless, but also a lot of system resources. Why? Because NSLog () is a function that essentially keeps stitching strings together. It is obvious that we cannot print in the post state.

So how do we get it to print in the debug state and not let it print when it's in the release state?

We can use the following macro to solve:

1 /* * * Log * * */ 2 // if it is running in debug mode, print; If not, do nothing . 3 #ifdef DEBUG 4 #define Jtlog (...) NSLog (__va_args__)5#else6#define jtlog (...) 7 #endif

One of the things to note is:

__va_args__: In general, the left macro ... The contents of the __va_args__ are copied in the right place. It is a variable-parameter macro that is new in the C99 specification.

OK, so the definition of the macro above is very clear. If the macro is in the debug mode (naturally, debug mode), then call Jtlog (...) This macro is exactly the same as calling NSLog (...); otherwise, calling Jtlog (...) equals doing nothing. When we need to print, just call Jtlog (...) instead of NSLog (...). The function is realized!

Wait a minute! What does the debug macro mean?

We tried to command+ the macro and found that the system could not find the location of the macro. Why is that?

In fact, this macro is in this position:

That is TARGETS--and Build Settings--Apple LLVM 8.0-language-modules---preprocessor Macros--Debug location.

As we can see, it sets a macro debug=1 by default. In release mode, there is no such macro. In fact, this macro is set here, so the system will not be found. At the same time, we can also set some macros here, the format is:

Macro=value

Note: The macro name set here cannot be all lowercase letters.

This sets up a macro that no one else can find. And you can call it from anywhere. But I do not recommend this, if you define a bunch of macros here, but your colleague or the person who took over the code can not find, you will be scolded-.-

It's that simple. But in the end there is another problem. How do I use this jtlog (...) in all classes?

Yes, nature is put in the prefixheader.pch-specific not detailed introduction, Baidu Bar.

Finally, we can also declare such a macro in prefixheader.pch to quickly print the current method name:

1 #define Jtlogfunc Jtlog (@ "%s", __func__);

When called, the semicolon can not be written.

2.Swift

Right, in fact, the beginning is the focus of the article. If you know how to achieve the objective-c in the custom printing of the Tao friends, you can directly look down, the beginner is swift Objective-c recommended tutorial under the next thing, some things I will not repeat the introduction.

The big difference between swift and objective-c is that there is no definition of macros in Swift! How is it good?

Indeed, there is no way to define macros in Swift, but in fact we can customize the macros. And the same way as the custom macros above.

This is the location.

  TARGETS -- Build Settings -- Swift complier-custom Flags --- Other Swift Flags -- DEBUG

Format-D debugswift. That is, declare the macro before you add one such symbol-D. It is automatically divided into two lines of display.

It is recommended to differentiate from the DEBUG macros above objective-c to avoid confusion.

The macro has, how to write the specific print?

Very simple. Declare a global function.

1 func jtprint<n>(message:n) {2     3     #if debugswift4      Print (message)
5 #endif   
6 }

Another sentence: Put this function outside of any class, that is, class{} outside, is the global function, the global variable is the same.

Or you can declare a message with an unusually full print, and use the predefined characters to add some other information you want to see. such as this:

1 #line) {2     3     #if // If in debug mode, the print 4         5         Print ("\ (fileName as NSString) \ n method: \ (methodName) \ n line number: \ (linenumber) \ n Print information \ (message)" ); 6     #endif 7 }

Printing results That's unusually cool:

1 file name: controller/smartscene-smart Scene/smartinhomeviewcontroller.swift2 method: Viewdidload ( )3 line number:4 printing information: 123

Objective-c and Swift Custom print functions (Debug and release)

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.