ファイル名や行数などを表示するための特殊変数の出力についてメモしておく。
正直なところ、どのように呼んだらいいかわからないため、「特殊文字」と書いておく
- ファイル名
- メソッド名
- 現在行数
ファイル名
この特殊変数を使ったファイルの名前を出力します。
ファイル名だけではなく、絶対パスが返されます。
メソッド名
この特殊変数を使ったメソッドの名前を出力します。
現在行数
この特殊変数を使った位置が何行目かを出力します。
サンプルコード
import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        print("------viewDidLoad start------")
        print("ファイル名:", #file)
        print("メソッド名:", #function)
        print("行数:", #line)
        print("------viewDidLoad end------")
        self.methodA()
    }
    func methodA() {
        print("------methodA start------")
        print("ファイル名:", #file)
        print("メソッド名:", #function)
        print("行数:", #line)
        print("------methodA end------")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
出力結果
——viewDidLoad start——
ファイル名: /Users/[user_name]/Desktop/develop/iOSApp/LogOutput/LogOutput/ViewController.swift
メソッド名: viewDidLoad()
行数: 12
——viewDidLoad end——
——methodA start——
ファイル名: /Users/[user_name]/Desktop/develop/iOSApp/LogOutput/LogOutput/ViewController.swift
メソッド名: methodA()
行数: 21
——methodA end——
ファイル名: /Users/[user_name]/Desktop/develop/iOSApp/LogOutput/LogOutput/ViewController.swift
メソッド名: viewDidLoad()
行数: 12
——viewDidLoad end——
——methodA start——
ファイル名: /Users/[user_name]/Desktop/develop/iOSApp/LogOutput/LogOutput/ViewController.swift
メソッド名: methodA()
行数: 21
——methodA end——
これはデバッグ用に使えると思う。
どのメソッドのどの部分を通ったとかをログに出力しておくとか。
いや、もしかしたらこのようなことを書く時代ではないのかな?(^^;
