Skip to content

Instantly share code, notes, and snippets.

@n-b
Created November 23, 2012 08:39
Show Gist options
  • Save n-b/4134555 to your computer and use it in GitHub Desktop.
Save n-b/4134555 to your computer and use it in GitHub Desktop.
More KVC weirdness with NSOrderedSets
//
// clang orderedsets-more-kvc.m -framework Foundation && ./a.out
//
#import <Foundation/Foundation.h>
int main ()
{
@autoreleasepool {
id data = (@[
@{ @"values": [NSOrderedSet orderedSetWithArray:@[@1, @2, @3, @4]] },
@{ @"values": [NSOrderedSet orderedSetWithArray:@[@1, @3, @5, @7]] }
]);
// valueForKeyPath logs an error, but somehow manages to return the correct result.
NSLog(@"union : %@",[data valueForKeyPath:@"@distinctUnionOfArrays.values"]);
/*
2012-11-23 09:34:47.507 a.out[12956:707] *** -[NSMutableArray addObjectsFromArray:]: array argument is not an NSArray
2012-11-23 09:34:47.509 a.out[12956:707] *** -[NSMutableArray addObjectsFromArray:]: array argument is not an NSArray
2012-11-23 09:34:47.509 a.out[12956:707] union : (
5,
1,
2,
7,
3,
4
)
*/
// Convert each OrderedSet to an Array to mute the error.
NSLog(@"union : %@",[data valueForKeyPath:@"@distinctUnionOfArrays.values.@array"]);
/*
2012-11-23 09:34:41.930 a.out[12927:707] union : (
5,
1,
2,
7,
3,
4
)
*/
}
return 0;
}
@Coeur
Copy link

Coeur commented Aug 6, 2015

Also discovered @set:

[data valueForKeyPath:@"@distinctUnionOfSets.values.@set"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment