Skip to content

Commit

Permalink
Don’t crash if sample is called on @[]
Browse files Browse the repository at this point in the history
Fixes #12

Tag 11.0.1
  • Loading branch information
mxcl committed Nov 23, 2014
1 parent 2b45ecd commit bbb5eb2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions NSArray+sample.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

@implementation NSArray (YOLO)

- (id)sample {
return self[arc4random_uniform((u_int32_t)self.count)];
- (id)sample {
return self.count == 0
? nil
: self[arc4random_uniform((u_int32_t)self.count)];
}

@end
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'
s.version = '11.0.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
4 changes: 4 additions & 0 deletions tests
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,10 @@ int main() {
});
}
- (void)testEmptySample {
XCTAssertNil(@[].sample);
}
@end
Expand Down

0 comments on commit bbb5eb2

Please sign in to comment.