Dustin Bachrach

mobile engineer / developer platform enthusiast / dog lover

Introducing OCUDL

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.

The OCUDL source is available on GitHub under the MIT License. Documentation is at CocoaDocs. Pull requests, bugs, and issues are welcome.

Example

OCUDL defines literals using prefixes or suffixes. Here, we’ve defined a UIColor literal that uses the # prefix.

1
2
// Creates a UIColor
myView.backgroundColor = $(#FE22AA);

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.

Experimental

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.

Using OCUDL

You can get OCUDL through CocoaPods. You can learn more about dependency management with CocoaPods, but we’ll cover the basics.

Install CocoaPods:

1
2
gem install cocoapods
pod setup

Create a Podfile, in your project directory. Add the following lines:

1
2
3
platform :ios

pod 'OCUDL'

Then install the Podfile:

1
pod install

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:

1
#import <OCUDL/OCUDL.h>

To create a literal for your class, first implement the OCUDLClass protocol.

1
@interface YourClass : NSObject <OCUDLClass>

Then, register a literal prefix or suffix for your class.

1
2
3
4
5
6
@implementation YourClass
+ (void)load
{
    [[OCUDLManager defaultManager] registerSuffix:@"your-suffix" forClass:[YourClass class]];
}
@end

Finally, implement the literal initializer.

1
2
3
4
5
6
7
- (id)initWithLiteral:(NSString*)literal suffix:(NSString*)suffix
{
    if (self = [super init]) {
        // ...
    }
    return self;
}

Now you can use your literal anywhere in your project.

1
YourClass *foo = $(555your-suffix);

Blocks

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
[[OCUDLManager defaultManager] registerPrefix:@"#" forBlock:^id(NSString *literal, NSString *prefix) {
    // return a new instance of some class
}];

Useful literals

OCUDL comes with many useful built-in literals for UIKit and Foundation types.

1
2
3
4
5
#import <OCUDL/OCUDLBuiltins.h>

// ...

[OCUDLBuiltins use];

After you [OCUDLBuiltins use], you can take advantage of all the built-in literals anywhere in your code.

NSNull

1
NSNull *n = $(null);

NSURL

1
2
NSURL *url = $(http:www.apple.com);
NSURL *url2 = $(https:www.gmail.com);

NSUUID

1
NSUUID *uuid = $(68753A44-4D6F-1226-9C60-0050E4C00067uuid);

UIColor

1
2
3
UIColor *color = $(#FE22AA);
UIColor *color2 = $(#FFF);
UIColor *color3 = $(#yellow);

UIImage

1
UIImage *img = $(pic.img);

UINib

1
UINib *nib = $(MyNib.xib);

UIStoryboard

1
UIStoryboard *board = $(MyBoard.storyboard);