如何快速打印变量名?

Ray*_*ond -1 swift swift5

我们如何快速打印变量名?它类似于这个问题所讨论的Get a Swift Variable's Actual Name as String 我无法从上面的 QA 中弄清楚如何快速打印它以满足我的需要,如下所述

struct API {
    static let apiEndPoint = "example.com/profile?"
    static let apiEndPoint1 = "example.com/user?"
 }

doSomething(endPoint: API.apiEndPoint)
doSomething(endPoint: API.apiEndPoint1)

func doSomething(endPoint: String) {
    // print the variable name which should be apiEndPoint or endPoint1
} 
Run Code Online (Sandbox Code Playgroud)

Joa*_*son 5

您可以将结构更改为枚举

enum API: String {
    case apiEndPoint = "example.com/profile?"
    case apiEndPoint1 = "example.com/user?"
 }


func doSomething(endPoint: API) {
    print("\(endPoint): \(endPoint.rawValue)")
}
Run Code Online (Sandbox Code Playgroud)

例子

doSomething(endPoint: .apiEndPoint)
Run Code Online (Sandbox Code Playgroud)

apiEndPoint:example.com/profile?