OCUDL is an experiment to bring user defined literals to Objective-C. A literal is a shorthand expression that creates a value of a certain type. For example,
25ul creates an unsigned long of 25, and
@"hello" creates an NSString of hello. User defined literals bring this brevity and expressivity to our own types.
Literals are usually baked right in to the compiler. OCUDL, however, does not extend the compiler; it simply uses what already exists in the Objective-C runtime. You can learn more about OCUDL’s internals at OCUDL In Depth.
OCUDL defines literals using prefixes or suffixes. Here, we’ve defined a UIColor literal that uses the # prefix.
The $ tells OCUDL to interpret this value as a user defined literal. If user defined literals were baked right into the language, you wouldn’t need the $ at all.
Explore the Useful Literals section for additional examples of literals for UIKit and Foundation types.
OCUDL is an experiment, and might not be appropriate for your project. Please read OCUDL In Depth to understand what’s happening behind the scenes.
Podfile, in your project directory. Add the following lines:
1 2 3
Then install the Podfile:
CocoaPods will create a
.xcworkspace for you if you haven’t already created one. You will need to open your code in XCode through the workspace file and not the
.xcodeproj file from now on.
Now your project is all set to use OCUDL. Get started by importing the header:
To create a literal for your class, first implement the
Then, register a literal prefix or suffix for your class.
1 2 3 4 5 6
Finally, implement the literal initializer.
1 2 3 4 5 6 7
Now you can use your literal anywhere in your project.
Sometimes you might want to add literals for classes you didn’t author. Instead of using categories, just use OCUDL’s support for blocks.
1 2 3
OCUDL comes with many useful built-in literals for UIKit and Foundation types.
1 2 3 4 5
[OCUDLBuiltins use], you can take advantage of all the built-in literals anywhere in your code.
1 2 3