dimanche 5 février 2017

How to check if an object is in array

In swift, how can i check if an object is in array?

I have a simple class like this;

class test: {
    private var _number: Int!
    private var _type: String!

    var number: Int {
            return _number
    }
    var type: String {
            return _type
    }

    init (number: Int, type: String) {
        self._number = number
        self._type = type
    }
}

Also i have this class;

class testRandom {

    private let _numberArr: [Int] = [1,2,3,4,5,6,7,8,9,10]
    private let _typeArr: [String] = ["x","y","z"]
    public private(set) var _testArr: [test] = []
    private var _randomTest: test!

    func randomTestPicker () {
            repeat {

                    let randomNumber = Int(arc4random_uniform(UInt32(self._numberArr.count)))
                    let randomType = Int(arc4random_uniform(UInt32(self._typeArr.count)))

                    self._randomTest = test(number: self._numberArr[randomNumber], type: self._typeArr[randomType])
            } while self._testArr.contains(_randomTest) 
    }
}

All i want to do is to pick different objects. Lets say i have x2,y4,x6,z3,z8,y2 in _testArr. When i call randomTestPicker, it should not pick x2 or z8. Because they are already in array.

I have tried contains as you see. However it did not work for me. Is there any solution that i can use for this purpose? Or what is the best way to do this?




Aucun commentaire:

Enregistrer un commentaire