18
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-04-02
func print(debug: Any = "",
           function: String = #function,
           file: String = #file,
           line: Int = #line) {
    var filename = file
    if let match = filename.range(of: "[^/]*$", options: .regularExpression) {
        filename = filename.substring(with: match)
    }
    Swift.print("Log:\(filename):L\(line):\(function) \(debug)")
}

使い方

    override func viewDidLoad() {
        super.viewDidLoad()
        print() // Log:ViewController.swift:L27:viewDidLoad()
        print(debug: "Hello.") // Log:ViewController.swift:L28:viewDidLoad() Hello.
    }

ファイル名、行数、関数名が表示されて便利

18
22
1

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
18
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?