This is a guest post by Boisy Pitre, Mobile Visionary and lead iOS developer at Affectiva. Here he talks to us about string manipulation and interpolation, the simplicity of Swift and the power of Apple’s compilers – and how with Swift, less is more.
This is a follow-up post to Boisy’s While It’s Compiling interview from iOSCon 2014, which you can read here.
One of the very cool things about Swift is probably the most mundane: string manipulation and interpolation. The fact that as a developer, we can juggle using strings as naturally as numeric types is a huge deal. In this article, I’ll show you just why this is important, and how it can be not only a time saver, but a bug saver too.
Back In My Day…
Us “old timers” love to start out telling stories with that phrase. In this case, “back in my day” is not that long ago when we were (and still are in most cases) using Objective-C as our primary development language. Objective-C inherits a lot from C, and one of the things it tried to improve on was strings. If you’ve ever worked with C, you’ll remember the obtuse functions we had to use just to concatenate two strings:
char s; strcpy(s, "This is a string. "); char *p = strcat(s, "And this is another!");
No kidding. If this is foreign to you, then be thankful. Such code is fraught with potential security issues, not to mention the real possibility of outright crashes. Granted, functions like
strncpy came along to limit the security risk by enforcing a maximum number of bytes to copy, but still remained unwieldy to use and slightly unnatural in appearance.
Objective-C did its best to make things better. Here’s the C code above, rewritten in the simplest Objective-C style:
NSString *s = @"This is a string. "; NSString *p = [NSString stringWithFormat:@"%@ And this is another!", s];
… still not elegant, but fairly descriptive, and safe from the dangers of accessing memory in a more direct fashion in C. And by God, we loved it. And still do.
If you think the above code is easy to understand, then you’ve probably seen it one thousand times over. If you think it’s way too complicated, then you’re probably right in line with the new crop of developers who have cut their teeth on scripting languages like Ruby and Python. No matter which camp you’re in, you have to appreciate the simplicity of this:
let s = "This is a string. " let p = s + " And this is another!"
Come on. This is just too easy, isn’t it? That’s all it takes to add two strings in Swift. I have to tell you, when you come from the old way, it almost feels like cheating. Deep down, you don’t trust the compiler and think all of this pretty, sugary stuff is eating up tons of CPU cycles. Where are the good old “bare metal” programming days going to?
You can relax, because Apple’s compiler wizards have worked hard to optimize these statements under the covers. Just bask in the enjoyment of all that typing you’re going to save.
Format Specifiers… Ugh!
Another win that we get in Swift is with the printing of constants or variables. How many times in your code do you do this? A lot. And it’s a lot of typing… and tedious to boot. Here’s a simple contrived example:
int x = 42; char *s = "life"; printf("The answer to %s is %d!\n", s, x);
Now multiply that times one hundred lines!
This has been the bane of C and Objective-C for a long time. Keeping track of what format specifiers to use for what types is a pain, but what’s even more of a pain is if you have a ton of them in one
printf statement, and forget a couple, or worse, transpose the variables so that the types don’t match. Compilers got smarter over time and warned you of such transgressions, but what a time killer nonetheless.
C++ tried to get it right, and did a decent job, but it still didn’t have that “natural” syntax flavor:
cout << "The answer to " << s << " is " << x << "!\n";
Nice try, C++, but Swift cuts through this problem like a hot knife in butter:
let x = 42 let s = "life" println("The answer to \(s) is \(x)!")
You may have to get used to typing a backslash followed by open and closing parentheses around your variable names, but this is clear and concise and has minimal chance of collision with your string characters.
Less is More
Strings are just one of many things that Apple has really made fun and easy to use in Swift. The theme of the language certainly seems to be “less is more.” Less typing and gaining more functionality. After all, computer languages are about us humans expressing ourselves more naturally in our work so that things can get done quicker.
If you haven’t started learning Swift yet, you should. If you have started, hold on to your bucket seats, because it’s going to be a wild but great ride!