Wednesday, June 9, 2021

Constant Literals in Objective-C

Andrew Madsen:

In 2012, with the release of Xcode 4.4 and LLVM 4.0, Apple introduced Objective-C literals for three more common Objective-C types: NSArray, NSDictionary, and NSNumber.


However, unlike NSStrings, these literals were just syntactic sugar for calls to the regular alloc/init methods at runtime, and as such they couldn’t be used to initialize global variables.


Xcode 13 ships with a new major release of Clang/LLVM, version 13. New in this release is support for constant literals for NSNumber, NSArray, and NSDictionary.


I take this to mean that even where these kinds of literals are not used to initialize global variables, ie. anywhere else they’re used in your code, the compiler may optimize them into the CONST section of your binary.


Apple has updated the plutil command line tool included with Xcode so that it can create Objective-C source files containing constant literals from plist data.


Comments RSS · Twitter

Leave a Comment