ios - Swift + macro parameters -
ios - Swift + macro parameters -
i read q&a related macros in swift, , did figure out everything in swift global, am right?
and actual question if have macro in need parameters pass how can pass in swift language?
e.g.
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 swift syntax above macro?
as 0o0o0o0 mentioned, macros in sense of "the compiler should see color_code(0, 0, 0, 1) , replace [uicolor colorwithred: 0/255.0 green: 0/255.0 blue: 0/255.0 alpha: 1]" not exist in swift.
macros in c can used in ways produce confusing error messages:
#define is_equal_to_me(argument) [self isequal: argument] bool func(id argument) { homecoming is_equal_to_me(argument); } // error: utilize of undeclared identifier 'self' or destroy readability:
#define open_block { #define close_with_exit_if_false } else { exit (0); } if (x < 0) open_block homecoming 10; close_with_exit_if_false for simple cases color_code commonly recommended strategy c utilize inline functions:
ns_inline uicolor *colorcode(cgfloat r, cgfloat g, cgfloat b, cgfloat a) { homecoming [uicolor colorwithred:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]; } this had same performance macro in inlined same code typed , not depend on macro processing rules. code has direct translation swift:
func colorcode(red:cgfloat, green:cgfloat, blue:cgfloat, alpha:cgfloat) -> uicolor { homecoming uicolor(red: red/255, green: green/255, blue: blue/255, alpha: alpha) } because "everything global" can declare in file , utilize in other file in same module. compiler in case decide whether or not inline function.
ios swift macros ios8
Comments
Post a Comment