Andrew Madsen - Constant Literals in Objective-C

submited by
Style Pass
2021-06-10 07:30:02

Starting with 2014, when Swift was announced, Objective-C material has been sparse in WWDC sessions, release notes, and new features . However, almost every year, there has been something new that makes life a little easier for those of us still using the language. WWDC 2021 is no exception. This year, clang has gained a new Objective-C feature called Constant Literals. In this post, I’ll explain what Constant Literals are and how to use them, along with some nice extra functionality added to the command-line plutil to work with them.

For as long as I’ve been writing Objective-C, NSString literals have been available. You can include a string in your program by surrounding the text of the string with double quotes prefixed with an @ symbol:

Strings created this way are stored in a TEXT section of the compiled binary. The compiler automatically uniques them, ensuring that there’s only one instance of a given string, no matter how many times it appears in the source code.

Leave a Comment