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)[@"http://hypercrypt.net"]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:$(NSURLRequest)[@"http://hypercrypt.net"] 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
AOCUDL includes a few literals for built-in classes, they broadly match those included by OCUDL. Examples:
$(NSMutableURLRequest)[@"http://hypercrypt.net"]- This creates a GET request
$(NSSet)[@[object1, object2, object3]]
$(NSURLRequest)[@"http://hypercrypt.net"]- This creates a GET request