> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/appendix/xcode/directives/file-line-function.md).

# #file, #line, #function

temp

{% tabs %}
{% tab title="💈#file" %}
💾 程式： [replit](https://replit.com/@pegasusroe/function-3#main.swift)

```swift
// ----------------------
//     ⭐ log function 
// ----------------------

func log(
    _ message: String,
    // file     : String = #file,
    line     : Int     = #line,
    function : String  = #function, 
    note     : String? = nil            // additional info
) {
    print("ℹ️ \(message)\n (func: '\(function)', line: \(line))\n " + (note ?? ""))
}

// ----------------------------------------
//     ⭐ top level: current module name
// ----------------------------------------

log("top level")

// ---------------------------------------------
//     ⭐ inside getter/setter: property name
// ---------------------------------------------

var freeVar: Int {
    get { log("inside getter"); return 0 }  // freeVar ⭐ 
    set { log("inside setter") }            // freeVar ⭐ 
}

// call getter/setter
let x = freeVar     
freeVar = 1         

// ------------------------------------------------------
//     ⭐ inside function: function name (with params)
// ------------------------------------------------------

func f(x: Int) {
    log("inside function")      // f(x:) ⭐ 
}

func add(_ a: Int, _ b: Int) { 
    log("inside function", note: "(column: \(#column))\n") // add(_:_:) ⭐ 
    // (line: 44, column: 47)
}

// call the functions
f(x: 1)  
add(1, 2)

// ---------------------------------------------
//     ⭐ inside init/subscript: that keyword
//     ⭐ inside method        : method name
// ---------------------------------------------

struct A {
    
    init(value: Int) {
        log("inside 'init'")            // init(value:) ⭐ 
    }
    subscript(index: Int) -> Int {
        log("inside 'subscript'")       // subscript(_:) ⭐ 
        return 0
    }
    
    func method(x: Int, y: Int) {
        log("inside method")            // method(x:y:) ⭐ 
    }
}

// call these members
let a = A(value: 1)
_ = a[0]
a.method(x: 1, y: 2)
```

{% endtab %}

{% tab title="📘 手冊" %}

* Swift Ref ⟩ Expressions ⟩ [Primary Expressions](https://docs.swift.org/swift-book/ReferenceManual/Expressions.html#ID389) ⟩ Literal Expression
  {% endtab %}

{% tab title="📗 參考" %}

* [x] Ficow Shen ⟩ [Swift 中的 #function 到底是什么？](https://blog.ficowshen.com/page/post/60)
  {% endtab %}

{% tab title="🗣 討論" %}

* [Weird value of #function literal in Swift 3.1](https://stackoverflow.com/a/45091446/5409815)
* [Macros in Swift?](https://stackoverflow.com/a/24116621/5409815) - custom log function using [#file, #line, #function](/ios/appendix/xcode/directives/file-line-function.md) ⭐️ (💈#file)
  {% endtab %}

{% tab title="👥 相關" %}

* [log()](/ios/swift/debugging/log.md) - use #file, #line ... to log debug message.
  {% endtab %}
  {% endtabs %}
