0

After using this code from a tutorial that apparently worked before, isn't working now using swift 3 but I'm not sure why. The error shows on the line: if !contains(uniqueValues, value as T)

extension Array {

    func unique<T: Equatable>() -> [T] {
        var uniqueValues = [T]();

        for value in self {
            if !contains(uniqueValues, value as T) {
                uniqueValues.append(value as! T);
            }
        }
        return uniqueValues;
    }

    func first<T>(test:(T) -> Bool) -> T? {
        for value in self {
            if test(value as! T) {
                return value as? T;
            }
        }
        return nil;
    }
}
Wain
  • 118,658
  • 15
  • 128
  • 151
Laurence Wingo
  • 3,912
  • 7
  • 33
  • 61
  • 2
    You're jumping from Swift 1.2 to Swift 3... that's a huge leap. :) There's many changes, you should read [the updated language guide](https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309). – Eric Aya Jul 05 '16 at 20:14
  • That `unique` impliementation is O(N^2), you should see http://stackoverflow.com/a/33553374/3141234 – Alexander Jul 05 '16 at 20:17
  • Another one here: http://stackoverflow.com/questions/32159295/contains-in-xcode-7-beta-5. – Martin R Jul 05 '16 at 20:45

1 Answers1

1

Since Swift 2, contains(_:) has been refactored into an instance method on Array:

if uniqueValues.contains(value as T)
Alexander
  • 59,041
  • 12
  • 98
  • 151