forked from soffes/sstoolkit
-
Notifications
You must be signed in to change notification settings - Fork 101
/
NSArray+SSToolkitAdditions.m
67 lines (46 loc) · 1.25 KB
/
NSArray+SSToolkitAdditions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// NSArray+SSToolkitAdditions.m
// SSToolkit
//
// Created by Sam Soffes on 8/19/09.
// Copyright 2009-2011 Sam Soffes. All rights reserved.
//
#import "NSArray+SSToolkitAdditions.h"
#import "NSData+SSToolkitAdditions.h"
@interface NSArray (SSToolkitPrivateAdditions)
- (NSData *)_prehashData;
@end
@implementation NSArray (SSToolkitAdditions)
- (id)firstObject {
if ([self count] == 0) {
return nil;
}
return [self objectAtIndex:0];
}
- (id)randomObject {
return [self objectAtIndex:arc4random() % [self count]];
}
- (NSArray *)shuffledArray {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
NSMutableArray *copy = [self mutableCopy];
while ([copy count] > 0) {
NSUInteger index = arc4random() % [copy count];
id objectToMove = [copy objectAtIndex:index];
[array addObject:objectToMove];
[copy removeObjectAtIndex:index];
}
[copy release];
return array;
}
- (NSString *)MD5Sum {
return [[self _prehashData] MD5Sum];
}
- (NSString *)SHA1Sum {
return [[self _prehashData] SHA1Sum];
}
@end
@implementation NSArray (SSToolkitPrivateAdditions)
- (NSData *)_prehashData {
return [NSPropertyListSerialization dataWithPropertyList:self format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil];
}
@end