Last active
August 29, 2015 14:05
-
-
Save zeroelink/a4f29a914290050d5ec9 to your computer and use it in GitHub Desktop.
NSString SyntacticSugar Category
This file contains hidden or 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
#define NUMARGS(...) (sizeof((id[]){__VA_ARGS__})/sizeof(id)) | |
#define $(...) ([NSString concatenateObjectsWithObjectCount:NUMARGS(__VA_ARGS__) objectList:__VA_ARGS__]) | |
#import <Foundation/Foundation.h> | |
@interface NSString (SyntacticSugar) | |
+ (instancetype)concatenateObjectsWithObjectCount:(NSInteger)count objectList:(id)firstObject, ...; | |
@end | |
@implementation NSString (SyntacticSugar) | |
+ (instancetype)concatenateObjectsWithObjectCount:(NSInteger)count objectList:(id)firstObject, ...{ | |
NSMutableArray *arguments = [NSMutableArray array]; | |
id object; | |
// pointer to variable argument list | |
va_list argumentList; | |
// set argumentList to point to firstObject | |
va_start(argumentList, firstObject); | |
object = firstObject; | |
while (true) { | |
// Only add object if its non-nil or not NSNull | |
if (object && object != [NSNull null]) { | |
[arguments addObject:object]; | |
} else { | |
count--; | |
} | |
// break once we've processed all valid objects in argumentList | |
if (arguments.count >= count) { | |
break; | |
} | |
// get the next argument on the list | |
object = va_arg(argumentList, id); | |
} | |
// release memory | |
va_end(argumentList); | |
return [arguments componentsJoinedByString:@""]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment