Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macros in Swift?

Tags:

macros

swift

Does Swift currently support macros, or are there future plans to add support? Currently I'm scattering:

Log.trace(nil, function: __FUNCTION__, file: __FILE__, line: __LINE__) 

In various places throughout my code.

like image 290
jhurliman Avatar asked Jun 09 '14 05:06

jhurliman


People also ask

What is #define in Swift?

Where you typically used the #define directive to define a primitive constant in C and Objective-C, in Swift you use a global constant instead. For example, the constant definition #define FADE_ANIMATION_DURATION 0.35 can be better expressed in Swift with let FADE_ANIMATION_DURATION = 0.35 .

What is macro and its syntax?

Writing a macro is another way of ensuring modular programming in assembly language. A macro is a sequence of instructions, assigned by a name and could be used anywhere in the program. In NASM, macros are defined with %macro and %endmacro directives.

What is macro in Objective C?

Macros can be used in many languages, it's not a specialty of objective-c language. Macros are preprocessor definitions. What this means is that before your code is compiled, the preprocessor scans your code and, amongst other things, substitutes the definition of your macro wherever it sees the name of your macro.

What is macro in compiler?

A macro is an automated input sequence that imitates keystrokes or mouse actions. A macro is typically used to replace a repetitive series of keyboard and mouse actions and used often in spreadsheets and word processing applications like MS Excel and MS Word.


Video Answer


1 Answers

In this case you should add a default value for the "macro" parameters.

Swift 2.2 and higher

func log(message: String,         function: String = #function,             file: String = #file,             line: Int = #line) {       print("Message \"\(message)\" (File: \(file), Function: \(function), Line: \(line))") }  log("Some message") 

Swift 2.1 and lower

func log(message: String,         function: String = __FUNCTION__,         file: String = __FILE__,         line: Int = __LINE__) {      print("Message \"\(message)\" (File: \(file.lastPathComponent), Function: \(function), Line: \(line))") }  log("Some message") 

This is what fatalError and assert functions do.

There are no other macros except the conditional compilation already mentioned in another answer.

like image 138
Sulthan Avatar answered Nov 20 '22 21:11

Sulthan