0
 class Test: NSObject {
    var product_id: String
    var size: String
    var color: String
    var desc: String
    var name: String

    init(arr: [String]) {
        product_id = arr[0]
        size = arr[1]
        color = arr[2]
        desc = arr[3]
        name = arr[4]
    }
}

let t1 = Test(arr: ["1", "S", "Blue", "111", "第一个商品"])
let t2 = Test(arr: ["1", "S", "Blue", "333", "第三个商品"])
let t3 = Test(arr: ["1", "S", "Blue", "666", "第六个商品"])
let t4 = Test(arr: ["1", "S", "Blue", "777", "第七个商品"])

let t5 = Test(arr: ["2", "S", "Blue", "222", "第二个商品"])
let t6 = Test(arr: ["2", "S", "Blue", "555", "第五个商品"])

let t7 = Test(arr: ["3", "S", "Blue", "444", "第四个商品"])

let initialSWSet = Set([t1, t2, t3, t4, t5, t6, t7])
initialSWSet.count

I expect initialSWSet.count == 3, in other words , if a test's product_id, size, color is same the test is same。 t1 == t2 == t3 == t4; t5 == t6; t7 how can i achieve the work? Thank advance!

巩小鹏
  • 69
  • 1
  • 6
  • What are you talking about? – Avi Jan 15 '16 at 08:29
  • We know in Swift, a custom class (not a NSObject subclass) support Set may implement Hashable & Equable protocols by self, but my class is a NSObject subclass. How can i achieve same work? – 巩小鹏 Jan 15 '16 at 08:44
  • 1
    Change the title of your question. "=" is assignment, not comparison. – gnasher729 Jan 15 '16 at 08:49
  • You *can* implement Hashable/Equatable in your NSObject subclass, compare http://stackoverflow.com/questions/33319959/nsobject-subclass-in-swift-hash-vs-hashvalue-isequal-vs. – Martin R Jan 15 '16 at 08:50
  • Thanks, i achieve it by override hash property and isEqul function @MartinR – 巩小鹏 Jan 15 '16 at 08:57
  • More simple, you can convert your object to nsdata and compair "==" between 2 nsdata – Nguyen Hoan Jan 15 '16 at 09:00

0 Answers0