Created
March 26, 2015 16:52
-
-
Save vilhalmer/9f4e484d9920f2f52e69 to your computer and use it in GitHub Desktop.
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
#import <Cocoa/Cocoa.h> | |
#define k_NSArrayTupleAssignment @"THISISAMAGICVALUELOL" | |
#define _(...) [NSArray arrayOfPointerValues:(void *)__VA_ARGS__, NULL][k_NSArrayTupleAssignment] | |
@interface NSArray (TupleAssignment) | |
+ (instancetype)arrayOfPointerValues:(void *)pointer1, ...; | |
- (id)objectForKeyedSubscript:(id<NSCopying>)key; | |
- (void)setObject:(id)object forKeyedSubscript:(id<NSCopying>)key; | |
@end |
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
#import "NSArray+TupleAssignment.h" | |
@implementation NSArray (TupleAssignment) | |
+ (instancetype)arrayOfPointerValues:(void *)pointer1, ... | |
{ | |
NSMutableArray * newArray; | |
if (!(newArray = [NSMutableArray array])) return nil; | |
va_list args; | |
va_start(args, pointer1); | |
NSUInteger index = 0; | |
void * currentPointer = pointer1; | |
while (currentPointer) { | |
newArray[index] = [NSValue valueWithPointer:currentPointer]; | |
currentPointer = va_arg(args, id __unsafe_unretained *); | |
++index; | |
} | |
va_end(args); | |
return [newArray copy]; | |
} | |
- (id)objectForKeyedSubscript:(id<NSCopying>)key | |
{ | |
if (![(NSString *)key isEqualToString:k_NSArrayTupleAssignment]) return nil; | |
NSMutableArray * properArray = [NSMutableArray array]; | |
for (NSUInteger i = 0; i < self.count; ++i) { | |
properArray[i] = (__bridge id)[self[i] pointerValue]; | |
} | |
return properArray; | |
} | |
- (void)setObject:(id)object forKeyedSubscript:(id<NSCopying>)key | |
{ | |
if (![(NSString *)key isEqualToString:k_NSArrayTupleAssignment]) return; | |
NSArray * values = (NSArray *)object; | |
for (NSUInteger i = 0; i < [values count]; ++i) { | |
id __unsafe_unretained * currentPointer = (id __unsafe_unretained *)[(NSValue *)self[i] pointerValue]; | |
*currentPointer = values[i]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment