Swift + macro options

I read all the Q&A related to macros in Swift

and I realized that everything in Swift now global,

Am I right?

And my actual question is, if I have a macro where I need parameters to pass, then how can I pass it in the Swift

language?

eg.

Objective-C Macro

#define COLOR_CODE(red, green, blue, alpha) [UIColor colorWithRed: red/255.0 green: green/255.0 blue: blue/255.0 alpha: alpha]

What is the syntax Swift

for the specified macro?

+1


source to share


3 answers


As 0O0O0O0 mentioned, macros in the sense of "The compiler should see COLOR_CODE(0, 0, 0, 1)

and replace it with [UIColor colorWithRed: 0/255.0 green: 0/255.0 blue: 0/255.0 alpha: 1]

" do not exist in Swift.

Macros in C can be used in ways that lead to confusing error messages:

#define IS_EQUAL_TO_ME(argument) [self isEqual: argument]

BOOL func(id argument) {
    return IS_EQUAL_TO_ME(argument);
}

// Error: Use of undeclared identifier 'self'

      

Or destroy readability:

#define OPEN_BLOCK {
#define CLOSE_WITH_EXIT_IF_FALSE } else { exit (0); }

if (x < 0)
OPEN_BLOCK
    return 10;
CLOSE_WITH_EXIT_IF_FALSE

      



For simple cases such as the COLOR_CODE

recommended strategy for C was to use built-in functions:

NS_INLINE UIColor *ColorCode(CGFloat r, CGFloat g, CGFloat b, CGFloat a) {
    return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a];
}

      

This had the same performance as a macro, since it would be tied to the same code, but strongly typed and independent of macro processing rules. This code also has a direct translation into Swift:

func ColorCode(red:CGFloat, green:CGFloat, blue:CGFloat, alpha:CGFloat) -> UIColor {
    return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: alpha)
}

      

Since everything is global, you can declare this in any file and use it in any other file in the same module. In this case, it is up to the compiler to decide whether to include the function.

+3


source


You will probably need to convert it to a function or generic, according to this documentation page :



"Complex macros are used in C and Objective-C, but have no analogues in Swift. Complex macros are macros that do not define constants, including function macros in parentheses. You use complex macros in C and Objective-C to avoid the constraints of type checking, or avoid retyping a lot 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 compromise. Therefore, complex macros that are in C and Objective-C source files are not available for your Swift code. "

+2


source


What I did was create a class method that returns #define.

Example:

.h file:

#define COLOR_CODE(red, green, blue, alpha) [UIColor colorWithRed: red/255.0 green: green/255.0 blue: blue/255.0 alpha: alpha]
+ (UIColor*)colorCodeWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;

      

.m file:

+ (UIColor*)colorCodeWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha { return COLOR_CODE(red, green, blue, alpha); }

      

And in Swift:

Since it is a class method, you can use it in much the same way as #define. If you change your #define macro - it will be reflected in the new method you created In Swift:

let color = YourClass.colorCodeWithRed (72.0, green: 55.0, blue: 100.0, alpha: 1.0);

+2


source







All Articles