From objective-c to Swift, you have to be. (d) DLog

Source: Internet
Author: User

The interrupt point is too slow when debugging, so the output log is a good choice. Breakpoint, a row of the press, too troublesome. From a single piece of log , it is quickly possible to find out which function is where the code is not executed. The debugging techniques are not discussed in detail here. But that's probably the way it is. However, the output log you only want to look at when debugging. In the official release of the release, who you look at those things, and how much the execution of the statement accounted for resources. So, this time to use the #ifdef Such processing commands, in the DEBUG output log, the release version of this thing.

DLog:

#ifdef debug#   define DLOG (FMT, ...) NSLog (@ "%s [line%d]" FMT), __pretty_function__, __line__, # #__VA_ARGS__); #else #   define DLOG (...) #endif

This macro definition can see that the name and line number of the function and other custom message contents are output at execution time. Therefore, the content of this DLog output is very effective when debugging.

There are also a lot of identifier built into Swift, and it's easy to implement the above macro. Of course, there is no macro definition in Swift, so it is only possible to use the most general function. Swift's implementation:

Func Dlog (message:string) {            #if DEBUG   println ("File: \ (__file__), line:\ (__line__)")    #endif}

It's nice to watch. Here's the story of a group of birds and a bird in a primary school textbook learning to build a house.

The above function is not available, because, hehe, the output filename and line number is always the same. is the name of the file where this function is located and the line number of the line that this function defines in this file! You can't solve your problem by seeing this function taken. So, look down.

One interesting feature of Swift is that this feature guarantees that the function outputs the name of the file that called the function and the line number of the call point Dlog. Come on, look at the code and you know it:

Func Dlog (message:string = "", file:string = __file__, function:string = __function__, Linenum:int = __line__) {//
   
     #if DEBUG        println ("FILE: \ (file.pathcomponents.last!), FUNC: \ (function), line: \ (linenum) MESSAGE: \ (Message)")/ /    #else        //Do nothing//    #endif}
   

This feature of Swift is a function parameter given a default value. __file__, __function__, etc. are set to the default value of the function. You can ignore these parameters directly when you call them again. At this point, the function outputs the file name and function name of the calling point and the line number where the call point is located.

Finally, the value of__file__ is the path to a completed file. This is not a need when we debug the code. Because your code file is in the box on the left side of Xcode, you don't have to go to the file location to handle it. We just need the file name to do it. So use file.pathComponents.last to the name of the file that contains the path to the live file.

From objective-c to Swift, you have to be. (d) DLog

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.