LoginSignup
9
5

More than 5 years have passed since last update.

【Swift4】DEBUG時のみprint()でファイル名、行数、関数名を出力する方法

Last updated at Posted at 2017-10-24

Swiftのprint()をファイル名、行数、関数名を出力して分かりやすくする

上記記事のコードをSwift4に対応し、DEBUG時のみ出力する形にいたしました。

func print(debug: Any = "", function: String = #function, file: String = #file, line: Int = #line) {
    #if DEBUG
        var filename: NSString = file as NSString
        filename = filename.lastPathComponent as NSString
        Swift.print("File: \(filename), Line: \(line), Func: \(function) \n\(debug)")
    #endif
}

使い方

ViewController.swift
override func viewDidLoad() {
    super.viewDidLoad()
    print() // File: ViewController.swift, Line: 44, Func: viewDidLoad()
    print(debug: "テスト") // File: ViewController.swift, Line: 44, Func: viewDidLoad() テスト
}
9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5