summaryrefslogtreecommitdiff
path: root/DNSwift/DNSwift/ArrayExtensions.swift
blob: 9ea00a45d9bfcbe343d6509f9289c32c974ce26e (plain)
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
68
69
70
71
72
73
74
75
76
77
78
//
//  Arrays.swift
//  DNSwift
//
//  Created by Matthew S on 6/8/14.
//  Copyright (c) 2014 Matthew S. All rights reserved.
//

//import Foundation

public extension Array {
    public func IndexOf<T: Equatable>(item: T) -> Int {
        //looks like this is the best way to do it - http://stackoverflow.com/questions/24028860/how-to-find-index-of-list-item-in-apples-swift/24029119#24029119
        
        for (index, val) in enumerate(self) {
            if(val as T == item) {
                return index;
            }
        }
        
        return -1;
    }
    
    public func Any() -> Bool {
        return self.count > 0;
    }
    
    public func Count() -> Int {
        return self.count;
    }
    
    public func Where(predicate: (T) -> Bool) -> Array<T> {
        return self.filter(predicate);
    }
    
    public func First() -> T {
        return self[0];
    }
    
    public func FirstOrDefault() -> T? {
        if (self.count > 0) {
            return self[0];
        }
        else {
            return nil;
        }
    }
    
    public func First(predicate: (T) -> Bool) -> T {
        return self.filter(predicate)[0];
    }
    
    public func FirstOrDefault(predicate: (T) -> Bool) -> T? {
        var found = self.filter(predicate);
        if(found.count > 0)
        {
            return found[0];
        }
        else
        {
            return nil;
        }
    }
}

/*public extension Array {// : IEnumerable {
    /*func GetEnumerator<IE:IEnumerator>() -> IE {
        return Enumerator(objs: self) as IE;
    }*/
    
    /*func Any(predicate: (T) -> Bool) {
    return self.Count(predicate) as Int > 0;
    }*/
    
    //public func ToList() -> List<T> {
        //return List(objs: self);
    //}
}*/
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback