Date
Nov. 21st, 2024
 
2024年 10月 21日

Post: iOS : Debug 与 print 002[Swift]

iOS : Debug 与 print 002[Swift]

Published 12:02 Feb 27, 2015.

Created by @ezra. Categorized in #Programming, and tagged as #iOS.

Source format: Markdown

Table of Content

接上文,本篇我们借鉴一下 JavaScript 中的 console.log:

//
//  Console.swift
//  Console
//
//  Created by Meniny on 15/7/27.
//  Copyright © 2015年 Meniny. All rights reserved.
//
import Foundation

class console {

    /*!
    *  @method log:
    *
    *  @discussion JavaScript like print method
    *
    */
    class func log<T>(message: T, file: String = __FILE__, method: String = __FUNCTION__, line: Int = __LINE__) {
        #if DEBUG
            print("* ----------------------\n* File: \((file as NSString).lastPathComponent)\n* Line: \(line)\n* Function: \(method)\n* \(message)\n")
        #else
            // save logs
        #endif
    }
}

与上一篇类似,我们的输出依然会额外添加文件名、方法名、行号等信息。

let abc = "def"
console.log("abc = \(abc)")
console.log("Some log")

看看输出结果:

* ----------------------
* File: ViewController.swift
* Line: 19
* Function: viewDidLoad()
* abc = def

* ----------------------
* File: ViewController.swift
* Line: 20
* Function: viewDidLoad()
* Some log
Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
开发、设计与写作皆为所长。
热爱苹果、钟情色彩。
随时恭候 垂询