RosettaCodeData/Task/Collections/Objective-C/collections.m

51 lines
1.1 KiB
Mathematica
Raw Permalink Normal View History

2013-04-10 22:43:41 -07:00
#import <Foundation/Foundation.h>
void show_collection(id coll)
{
2014-04-02 16:56:35 +00:00
for ( id el in coll )
2013-04-10 22:43:41 -07:00
{
2014-04-02 16:56:35 +00:00
if ( [coll isKindOfClass: [NSCountedSet class]] ) {
NSLog(@"%@ appears %lu times", el, [coll countForObject: el]);
2013-04-10 22:43:41 -07:00
} else if ( [coll isKindOfClass: [NSDictionary class]] ) {
2014-04-02 16:56:35 +00:00
NSLog(@"%@ -> %@", el, coll[el]);
2013-04-10 22:43:41 -07:00
} else {
NSLog(@"%@", el);
}
}
printf("\n");
}
int main()
{
2014-04-02 16:56:35 +00:00
@autoreleasepool {
2013-04-10 22:43:41 -07:00
2014-04-02 16:56:35 +00:00
// create an empty set
NSMutableSet *set = [[NSMutableSet alloc] init];
// populate it
[set addObject: @"one"];
[set addObject: @10];
[set addObjectsFromArray: @[@"one", @20, @10, @"two"] ];
// let's show it
show_collection(set);
2013-04-10 22:43:41 -07:00
2014-04-02 16:56:35 +00:00
// create an empty counted set (a bag)
NSCountedSet *cset = [[NSCountedSet alloc] init];
// populate it
[cset addObject: @"one"];
[cset addObject: @"one"];
[cset addObject: @"two"];
// show it
show_collection(cset);
2013-04-10 22:43:41 -07:00
2014-04-02 16:56:35 +00:00
// create a dictionary
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
// populate it
dict[@"four"] = @4;
dict[@"eight"] = @8;
// show it
show_collection(dict);
2013-04-10 22:43:41 -07:00
2014-04-02 16:56:35 +00:00
}
2013-04-10 22:43:41 -07:00
return EXIT_SUCCESS;
}