[Mac] Swiftで関数名や行番号などを出力する

次のデバッグ用のリテラルを用いると、ファイル名や関数名などデバッグに役立つ情報を出力することが出来ます。

swift2までは、

print(“__FILE__ = \(__FILE__)”)
print(“__LINE__ = \(__LINE__)”)
print(“__COLUMN__ = \(__COLUMN__)”)
print(“__FUNCTION__ = \(__FUNCTION__)”)

上記のリテラルを使用できましたが、これらはswift3では廃止される予定らしく、実行すると

__FILE__ is deprecated and will be removed in Swift 3, please use #file
__LINE__ is deprecated and will be removed in Swift 3, please use #line
__COLUMN__ is deprecated and will be removed in Swift 3, please use #column
__FUNCTION__ is deprecated and will be removed in Swift 3, please use #function

このような警告が表示されます。そのため、今後は以下のリテラルを使用する必要があります。

print(“#file = \(#file)”)
print(“#line = \(#line)”)
print(“#column = \(#column)”)
print(“#function = \(#function)”)
  • #file : ソースファイルのファイルパス
  • #line : ファイル中の行番号
  • #column : 列番号(行の中で何文字目か)
  • #function : 関数名

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です