Created
June 13, 2016 10:08
-
-
Save smic/60505739ed30d15421dd020ce8ab7a1f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
class Object1: Protocol1, CustomStringConvertible { | |
var description: String { return "<\(String(self.dynamicType)):\(unsafeAddressOf(self))>" } | |
} | |
protocol Protocol1: class { | |
} | |
let object1 = Object1() | |
let object2 = Object1() | |
public final class SMAnyObjectBox: CustomStringConvertible, Hashable { | |
public init(_ value: AnyObject) { self.value = value } | |
public let value: AnyObject | |
public var description: String { return String(value) } | |
public var hashValue: Int { return ObjectIdentifier(value).hashValue } | |
} | |
public func ==(lhs: SMAnyObjectBox, rhs: SMAnyObjectBox) -> Bool { | |
return lhs.value === rhs.value | |
} | |
public extension Array where Element: AnyObject { | |
public func uniqueElements() -> [Element] { | |
let boxedElements = self.map({ SMAnyObjectBox($0) }) | |
let uniqueBoxedElements = Set(boxedElements) | |
let uniqueElements = uniqueBoxedElements.flatMap({ $0.value as? Element }) | |
return uniqueElements | |
} | |
} | |
let array1: [Object1] = [object1, object2, object1] | |
let uniqueArray1 = array1.uniqueElements() | |
let array2: [Protocol1] = [object1, object2, object1] | |
let uniqueArray2 = array2.uniqueElements() // <- Using 'Protocol1' as a concrete type conforming to protocol 'AnyObject' is not supported |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment