objective c - How can you implement this multiline string literal macro in Swift? -
in objective-c code gpuimage framework, have next macro:
#define stringize(x) #x #define stringize2(x) stringize(x) #define shader_string(text) @ stringize2(text)
which allows me inline multiline vertex , fragment shaders nsstring literals within custom filter subclasses, this:
nsstring *const kgpuimagepassthroughfragmentshaderstring = shader_string ( varying highp vec2 texturecoordinate; uniform sampler2d inputimagetexture; void main() { gl_fragcolor = texture2d(inputimagetexture, texturecoordinate); } );
gpuimage needs in order provide formatted vertex , fragment shaders included in body text of filter subclasses. shipping them separate files create framework unable compiled static library. using above macro, can create these shaders able copied , pasted between framework code , external shader files without ridiculous amount of reformatting work.
swift away compiler macros, , documentation has say:
complex macros used in c , objective-c have no counterpart in swift. complex macros macros not define constants, including parenthesized, function-like macros. utilize complex macros in c , objective-c avoid type-checking constraints or avoid retyping big amounts of boilerplate code. however, macros can create debugging , refactoring difficult. in swift, can utilize functions , generics accomplish same results without compromises. therefore, complex macros in c , objective-c source files not made available swift code.
per line "in swift, can utilize functions , generics accomplish same results without compromises", there way in swift provide multiline string literals without resorting string of concatenation operations?
alas swift multiline strings still not available, far know. when doing research regarding this, found workaround useful. combination of these items:
a quick hack quote swift strings in playground - describing how create service replacing , fixing texts the comment pyrtsa, regarding using
"\n".join(...)
emulate multiline strings setup automated service
using automator set service next properties:
a single action of "run shell script" tick off "output replaces selected text" change shell
/usr/bin/perl
add code excerpt below action window save "replace quoted swift multiline join" code excerpt
print "\"\\n\".join([\n"; # start bring together operation # each line, reformat , print while(<>) { print " "; # little indentation chomp; # loose newline s/([\\\"])/\\$1/g; # replace \ , " escaped variants print "\"$_\""; # add together quotes around line print "," unless eof # add together comma, unless lastly line print "\n"; # end line, preserving original line count } print " ])"; # close bring together operation
you of course of study free utilize whatever shell , code want, chose perl familiar me, , here comments:
i used
"\n".join(...)
version create multiline string, utilize extension reply swift - split string on multiple lines, or
+
variant, i'll leave exercise user i opted little indentation spaces, , replace
\
,
"
create little sturdier comments of course of study optional, , shorten code somewhat. tried opt clarity , readability the code, is, preserves spaces, edited if not wanted. left exercise user usage of service
open playground or code editor, , insert/write multline text:
mark text block execute xcode (or similar) > services > replace quoted swift multiline join
you have multiline string in proper swift coding. here illustration of before , after text:
here multiline text illustration both " , \ within text "\n".join([ "here multiline text ", "example both \" and", "a \\ within text" ])
objective-c swift