HDNZ HDNZ - 7 months ago 18
Swift Question

Declaring Global Macro in Swift

I want to declare a macro function like on Objective C

#define SIGNATURE_END() { if([[Config sharedInstance] checkConsoleLevel:ConsoleLogVerbose] == YES) { NSString *message = [NSString stringWithFormat:@"<< %s",__PRETTY_FUNCTION__]; Verbose(message); } }


This code I want to write with swift?

Answer

You want a kind of debug output. It's done in Swift like so:

func __enter(file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) {
    print("\(file):\(line):\(column) >> \(function)")
}

func __leave(file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) {
    print("\(file):\(line):\(column) << \(function)")
}

func xxx() {
    __enter()
    print("xxx")
    __leave()
}

xxx()

/*
/var/folders/mz/4rdq8yg95v56bhbndfvnr7z4lwl1px/T/lldb/875/playground162.swift:13:12 >> xxx()
xxx
/var/folders/mz/4rdq8yg95v56bhbndfvnr7z4lwl1px/T/lldb/875/playground162.swift:15:12 << xxx()
*/

The exact code obviously depends on your specific needs which you don't explain well. Hope this will help.