AOCUDL - Alternative Objective-C User Defined Literals

Alternative Objective-C User Defined Literals

AOCUDL was inspired by Dustin Bachrach's OCUDL.

The purpose of AOCUDL is to provide a simple way to define custom literals for Objective-C. These are correctly typecast, extensible and flexible.

The source is available on GitHub.

To add this to your project using CocoaPods just add the following to your podfile:

pod 'AOCUDL', '~> 1.0'


NSURLRequest *request = $(NSURLRequest)[@""];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

or simply

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:$(NSURLRequest)[@""] delegate:self];

This functionality is provided by using Objective-C subscripting and a small macro to glue the whole thing together:

#define $(cls) (cls *)((id)[cls class])

For a custom class to support AOCUDL all that needs to be implemented is one (or both) of the following methods:

+ (instancetype)objectAtIndexedSubscript:(NSUInteger)index;
+ (instancetype)objectForKeyedSubscript:(id)key;

The default implementation on NSObject is to return nil.

Useful Literals

AOCUDL includes a few literals for built-in classes, they broadly match those included by OCUDL. Examples:

  • NSMutableURLRequest: $(NSMutableURLRequest)[@""] - This creates a GET request
  • NSNull: $NSNull or $(NSNull)[any-id-or-NSUInteger]
  • NSSet: $(NSSet)[@[object1, object2, object3]]
  • NSURLRequest: $(NSURLRequest)[@""] - This creates a GET request
  • NSURL: $(NSURL)[@""]
  • NSUUID: $(NSUUID)[@"8DFD5DF3-6D8B-4DC4-B0A2-1F8ED614FC16"]
  • UIColor: $(UIColor)[0xff0000]
  • UIImage: $(UIImage)[@"testImage"]
  • UINib: $(UINib)[@"AOCUDLViewController"]
  • UIStoryboard: $(UIStoryboard)[@"Main_iPhone"]
comments powered by Disqus