Use imported C-defined macros as constants.
Swift automatically imports simple, constant-like macros, declared with the
#define directive, as global constants. Macros are imported when they use literals for string, floating-point, or integer values, or use operators like
== between literals or previously defined macros. This example defines some simple macros in a C header:
When imported into Swift, the macros in the above example are equivalent to these constant declarations:
Use Functions and Generics Instead of Complex Macros
C macros that are more complex than simple constant definitions have no counterpart in Swift. You use complex macros in C and Objective-C to avoid type-checking constraints or to avoid retyping large amounts of boilerplate code. However, macros can make debugging and refactoring difficult. In Swift, you can use functions and generics to achieve the same results without any compromises.