Skip to content

Commit

Permalink
Fixes #15; NS*Array is a can of worms
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Feb 2, 2015
1 parent 5c77fba commit 78ab423
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
11 changes: 9 additions & 2 deletions NSArray+map.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@implementation NSArray (YOLO)

- (NSArray *(^)(id))map {
return ^(id frock) {
return ^id(id frock) {
NSMethodSignature *sig = YOLOMS(frock);
id (^block)(id, NSUInteger) = ^{
switch (sig.numberOfArguments){
Expand All @@ -28,7 +28,14 @@ @implementation NSArray (YOLO)
if (o)
mapped[jj++] = o;
}
return [[self.class alloc] initWithObjects:mapped count:jj];

if ([self respondsToSelector:@selector(initWithObjects:count:)]) {
return [[self.class alloc] initWithObjects:mapped count:jj];
} else {
// some secret implementations of NSArray don't respond to the
// above selector.
return [NSArray arrayWithObjects:mapped count:jj];
}
};
}

Expand Down
8 changes: 6 additions & 2 deletions NSSet+fmap.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
@implementation NSSet (YOLO)

- (NSSet *(^)(id (^)(id)))fmap {
return ^(id (^block)(id)) {
return ^id(id (^block)(id)) {
id mapped[self.count];
NSUInteger ii = 0;
for (id mappable in self) {
id o = block(mappable);
if (o)
mapped[ii++] = o;
}
return [[self.class alloc] initWithObjects:mapped count:ii];
if ([self respondsToSelector:@selector(initWithObjects:count:)]) {
return [[self.class alloc] initWithObjects:mapped count:ii];
} else {
return [[NSSet alloc] initWithObjects:mapped count:ii];
}
};
}

Expand Down
2 changes: 1 addition & 1 deletion YOLOKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'YOLOKit'
s.version = '11.1'
s.version = '11.1.1'
s.source = { :git => 'https://github.com/mxcl/YOLOKit.git', :tag => s.version }
s.requires_arc = true
s.summary = 'A delightful library for enumerating Foundation objects.'
Expand Down

0 comments on commit 78ab423

Please sign in to comment.