mirror of
https://github.com/samsonjs/samhuri.net-ios.git
synced 2026-04-14 12:46:02 +00:00
84 lines
1.8 KiB
Objective-C
84 lines
1.8 KiB
Objective-C
//
|
|
// NSSet+ObjectiveSugar.m
|
|
// SampleProject
|
|
//
|
|
// Created by Marin Usalj on 11/23/12.
|
|
// Copyright (c) 2012 @mneorr | mneorr.com. All rights reserved.
|
|
//
|
|
|
|
#import "NSSet+ObjectiveSugar.h"
|
|
#import "NSArray+ObjectiveSugar.h"
|
|
|
|
@implementation NSSet (ObjectiveSugar)
|
|
|
|
- (id)first {
|
|
NSArray *allObjects = self.allObjects;
|
|
|
|
if (allObjects.count > 0)
|
|
return allObjects[0];
|
|
return nil;
|
|
}
|
|
|
|
- (id)last {
|
|
return self.allObjects.lastObject;
|
|
}
|
|
|
|
- (id)sample {
|
|
return [self.allObjects sample];
|
|
}
|
|
|
|
- (void)each:(void (^)(id))block {
|
|
[self enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
|
|
block(obj);
|
|
}];
|
|
}
|
|
|
|
- (void)eachWithIndex:(void (^)(id, int))block {
|
|
__block int counter = 0;
|
|
[self enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
|
|
block(obj, counter);
|
|
counter ++;
|
|
}];
|
|
}
|
|
|
|
- (NSArray *)map:(id (^)(id object))block {
|
|
NSMutableArray *array = [NSMutableArray arrayWithCapacity:self.count];
|
|
|
|
for (id object in self) {
|
|
id newObject = block(object);
|
|
[array addObject:newObject];
|
|
}
|
|
|
|
return array;
|
|
}
|
|
|
|
- (NSArray *)select:(BOOL (^)(id object))block {
|
|
NSMutableArray *array = [NSMutableArray arrayWithCapacity:self.count];
|
|
|
|
for (id object in self) {
|
|
if (block(object)) {
|
|
[array addObject:object];
|
|
}
|
|
}
|
|
|
|
return array;
|
|
}
|
|
|
|
- (NSArray *)reject:(BOOL (^)(id object))block {
|
|
NSMutableArray *array = [NSMutableArray arrayWithCapacity:self.count];
|
|
|
|
for (id object in self) {
|
|
if (block(object) == NO) {
|
|
[array addObject:object];
|
|
}
|
|
}
|
|
|
|
return array;
|
|
}
|
|
|
|
- (NSArray *)sort {
|
|
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES];
|
|
return [self sortedArrayUsingDescriptors:@[sortDescriptor]];
|
|
}
|
|
|
|
@end
|