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
  • Loading branch information
mxcl committed Nov 23, 2014
1 parent 2b45ecd commit ba3062d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 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
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 ba3062d

Please sign in to comment.