Created
March 22, 2012 03:18
-
-
Save pcperini/2155387 to your computer and use it in GitHub Desktop.
Quickly Make Objects out of Primitives
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Objectify.h | |
// | |
// Created by Patrick Perini on 3/21/12. | |
// Copyright (c) 2012 Inspyre Technologies. MIT License | |
// | |
#import <Foundation/Foundation.h> | |
id __objectify__(char *type, ...); | |
#ifndef $ | |
#define $(var) __objectify__(@encode(typeof(var)), var) | |
#endif | |
#ifndef objectify | |
#define objectify(var) __objectify__(@encode(typeof(var)), var) | |
#endif | |
/* | |
Example Use: | |
[NSArray arrayWithObjects: | |
$(54), | |
$(someCString), | |
$(someFloat), | |
$(@selector(myMethod:)), | |
$(YES), | |
nil | |
]; | |
// or even better | |
@[ | |
$(54), | |
$(someCString), | |
$(someFloat), | |
$(@selector(myMethod:)), | |
$(YES) | |
]; | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Objectify.m | |
// | |
// Created by Patrick Perini on 3/21/12. | |
// Copyright (c) 2012 Inspyre Technologies. MIT License. | |
// | |
#import "Objectify.h" | |
id __objectify__(char *type, ...) | |
{ | |
va_list args; | |
va_start(args, type); | |
id objectValue = nil; | |
switch (type[0]) | |
{ | |
case 'c': objectValue = [NSNumber numberWithChar: va_arg(args, int)]; break; | |
case 'i': objectValue = [NSNumber numberWithInt: va_arg(args, int)]; break; | |
case 's': objectValue = [NSNumber numberWithShort: va_arg(args, int)]; break; | |
case 'q': objectValue = [NSNumber numberWithLong: va_arg(args, long)]; break; | |
case 'f': objectValue = [NSNumber numberWithFloat: va_arg(args, double)]; break; | |
case 'd': objectValue = [NSNumber numberWithDouble: va_arg(args, double)]; break; | |
case 'B': objectValue = [NSNumber numberWithBool: va_arg(args, int)]; break; | |
case '*': objectValue = [NSString stringWithUTF8String: va_arg(args, char *)]; break; | |
case '@': objectValue = va_arg(args, id); break; | |
case '#': objectValue = va_arg(args, Class); break; | |
case ':': objectValue = NSStringFromSelector(va_arg(args, SEL)); break; | |
case '^': objectValue = [NSNull null]; break; | |
} | |
va_end(args); | |
return objectValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment