You can use the Swift Set :
let array = [product,product2,product3] let set = Set(array)
You have to make Product conform to Hashable (and therefore Equatable ), though:
class Product : Hashable { var subCategory = "" var hashValue: Int { return subCategory.hashValue } } func ==(lhs: Product, rhs: Product) -> Bool { return lhs.subCategory == rhs.subCategory }
And if Product was a subclass of NSObject , you should override isEqual :
override func isEqual(object: AnyObject?) -> Bool { if let product = object as? Product { return product == self } else { return false } }
Obviously, change those that reflect other properties that you might have in your class. For instance:
class Product : Hashable { var category = "" var subCategory = "" var hashValue: Int { return [category, subCategory].hashValue } } func ==(lhs: Product, rhs: Product) -> Bool { return lhs.category == rhs.category && lhs.subCategory == rhs.subCategory }
source share