Skip to content

Instantly share code, notes, and snippets.

@jaderfeijo
Last active March 1, 2016 16:39
Show Gist options
  • Select an option

  • Save jaderfeijo/b309dd243e6eeca20552 to your computer and use it in GitHub Desktop.

Select an option

Save jaderfeijo/b309dd243e6eeca20552 to your computer and use it in GitHub Desktop.
NSArray+Random
//
// Created by Jader Feijo on 03/12/2014.
// Copyright (c) 2014 Movinpixel. All rights reserved.
//
// @license MIT
#import <Foundation/Foundation.h>
@interface NSArray (Random)
- (id)randomObject;
@end
//
// NSArray+Random.m
// iFreeXDaily
//
// Created by Jader Feijo on 03/12/2014.
// Copyright (c) 2014 Movinpixel. All rights reserved.
//
// @license MIT
#import "NSArray+Random.h"
#import "NSNumber+Random.h"
@implementation NSArray (Random)
- (id)randomObject {
if ([self count] > 0) {
return [self objectAtIndex:[NSNumber randomIntegerBetween:0 and:([self count] - 1)]];
} else {
return nil;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment