Skip to content

Instantly share code, notes, and snippets.

@n-b
Created November 23, 2012 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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;
}
@woolsweater
Copy link

Do you have any idea where this @array operator comes from or why it needs to be in the last position? I can't find any mention of such an operator; there's the array property on NSOrderedSet, but that doesn't work as part of the path without the @. Very strange.

If you have a Stack Overflow account and any insight into this issue, please have a look at http://stackoverflow.com/questions/31780983/extracting-properties-from-nsarray-of-nsarray-of-nsdictionary/

@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