Daij-Djan
Daij-Djan

Reputation: 50119

Swift arrays and contains, how to determine if a collection contains an object or value?

I'm at it again with swift arrays and containsObject provided by NSArray only!

I bridge the swift array to NSArray to do that contains:

extension Array {
    func contains(object:AnyObject!) -> Bool {
        if(self.isEmpty) {
            return false
        }
        let array: NSArray = self.bridgeToObjectiveC();
        return array.containsObject(object)
    }
}

it works fine in general but as soon as I put a String! in an array of type String, it crashes. Even though containsObject does take a AnyObject!

        var str : String! = "bla"
        var c = Array<String>();
        c.append(str)
        println(c.contains(str))

declaring a String! array also doesn't help

        var str : String! = "bla"
        var c = Array<String!>();
        c.append(str)
        println(c.contains(str))

BUT the same without ! works fine

        var str : String = "bla"
        var c = Array<String>();
        c.append(str)
        println(c.contains(str))

SO how do I explicitly wrap stuff? I don't really see why I'd have to explicitly wrap it only so it is right unwrapped but that's what it looks like.

Upvotes: 33

Views: 46101

Answers (3)

azhman_adam
azhman_adam

Reputation: 31

Generally, when you want to have an array that contains a custom object or struct, and you want to work with "contains" function, your class or struct should be conformed to "Equatable" protocol and you should implement the "==" function for later comparisons...

struct booy: Equatable{
static func == (lhs: booy, rhs: booy) -> Bool {
    return lhs.name == rhs.name
}

var name = "abud"
}

let booy1 = booy(name: "ali")
let booy2 = booy(name: "ghasem")

var array1 = [booy]()
array1.append(booy1)
array1.append(booy2)

let booy3 = booy(name: "ali")

if array1.contains(booy3){
    print("yes") }

Upvotes: 1

jervine10
jervine10

Reputation: 3077

Swift 1:

let array = ["1", "2", "3"]
let contained = contains(array, "2")
println(contained ? "yes" : "no")

Swift 2, 3, 4:

let array = ["1", "2", "3"]
let contained = array.contains("2")
print(contained ? "yes" : "no")

Upvotes: 76

Anit Kumar
Anit Kumar

Reputation: 8153

Swift

If you are not using object then you can user this code for contains.

let elements = [ 10, 20, 30, 40, 50]

if elements.contains(50) {

   print("true")

}

If you are using NSObject Class in swift. This variables is according to my requirement. you can modify for your requirement.

var cliectScreenList = [ATModelLeadInfo]()
var cliectScreenSelectedObject: ATModelLeadInfo!

This is for a same data type.

{ $0.user_id == cliectScreenSelectedObject.user_id }

If you want to AnyObject type.

{ "\($0.user_id)" == "\(cliectScreenSelectedObject.user_id)" }

Full condition

if cliectScreenSelected.contains( { $0.user_id == cliectScreenSelectedObject.user_id } ) == false {

cliectScreenSelected.append(cliectScreenSelectedObject)

print("Object Added")

} else {

print("Object already exists")

}

Upvotes: 3

Related Questions