ใน Objective C คุณสามารถบันทึกวิธีการที่ถูกเรียกโดยใช้:
NSLog(@"%s", __PRETTY_FUNCTION__)
โดยปกติจะใช้จากมาโครการบันทึก
แม้ว่า Swift จะไม่รองรับมาโคร (ฉันคิดว่า) ฉันยังคงต้องการใช้คำสั่งบันทึกทั่วไปที่มีชื่อของฟังก์ชันที่ถูกเรียก เป็นไปได้ไหมใน Swift
อัปเดต: ตอนนี้ฉันใช้ฟังก์ชันส่วนกลางนี้สำหรับการบันทึกซึ่งสามารถพบได้ที่นี่: https://github.com/evermeer/Stuff#print และคุณสามารถติดตั้งโดยใช้:
pod 'Stuff/Print'
นี่คือรหัส:
public class Stuff {
    public enum logLevel: Int {
        case info = 1
        case debug = 2
        case warn = 3
        case error = 4
        case fatal = 5
        case none = 6
        public func description() -> String {
            switch self {
            case .info:
                return "❓"
            case .debug:
                return "✳️"
            case .warn:
                return "⚠️"
            case .error:
                return "🚫"
            case .fatal:
                return "🆘"
            case .none:
                return ""
            }
        }
    }
    public static var minimumLogLevel: logLevel = .info
    public static func print<T>(_ object: T, _ level: logLevel = .debug, filename: String = #file, line: Int = #line, funcname: String = #function) {
        if level.rawValue >= Stuff.minimumLogLevel.rawValue {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss:SSS"
            let process = ProcessInfo.processInfo
            let threadId = "?"
            let file = URL(string: filename)?.lastPathComponent ?? ""
            Swift.print("\n\(level.description()) .\(level) ⏱ \(dateFormatter.string(from: Foundation.Date())) 📱 \(process.processName) [\(process.processIdentifier):\(threadId)] 📂 \(file)(\(line)) ⚙️ \(funcname) ➡️\r\t\(object)")
        }
    }
}
ซึ่งคุณสามารถใช้ได้ดังนี้:
Stuff.print("Just as the standard print but now with detailed information")
Stuff.print("Now it's a warning", .warn)
Stuff.print("Or even an error", .error)
Stuff.minimumLogLevel = .error
Stuff.print("Now you won't see normal log output")
Stuff.print("Only errors are shown", .error)
Stuff.minimumLogLevel = .none
Stuff.print("Or if it's disabled you won't see any log", .error)    
ซึ่งจะส่งผลให้:
✳️ .debug ⏱ 02/13/2017 09:52:51:852 📱 xctest [18960:?] 📂 PrintStuffTests.swift(15) ⚙️ testExample() ➡️
    Just as the standard print but now with detailed information
⚠️ .warn ⏱ 02/13/2017 09:52:51:855 📱 xctest [18960:?] 📂 PrintStuffTests.swift(16) ⚙️ testExample() ➡️
    Now it's a warning
🚫 .error ⏱ 02/13/2017 09:52:51:855 📱 xctest [18960:?] 📂 PrintStuffTests.swift(17) ⚙️ testExample() ➡️
    Or even an error
🚫 .error ⏱ 02/13/2017 09:52:51:855 📱 xctest [18960:?] 📂 PrintStuffTests.swift(21) ⚙️ testExample() ➡️
    Only errors are shown
    
NSLog("Running %@ : %@",NSStringFromClass(self.dynamicType),__FUNCTION__)