33

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • the edited f***ed up this post – Daij-Djan Jun 15 '15 at 20:29
  • Do you have a suggestion to improve the edit? The question asked in the title was not clear. – jervine10 Jun 18 '15 at 13:49
  • This isn't the question I asked though ;) and no I don't have an idea im afraid – Daij-Djan Jun 18 '15 at 14:09
  • Regarding your original question, Daij-Djan, you should have either tested `if object == nil { return true }` or changed signature to `func contains(object:AnyObject)`. Casting from an Optional `String!` to an Optional `AnyObject!` will always fail because they are two incompatible enums. – Cœur Jun 19 '18 at 16:20
  • you realize this was 4 years ago? :D – Daij-Djan Jun 19 '18 at 16:21

3 Answers3

76

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")
Cœur
  • 37,241
  • 25
  • 195
  • 267
jervine10
  • 3,039
  • 22
  • 35
  • @jervine10 I doubt it will be. Arrays in Swift really are just plain old arrays. NSArrays are more like ArrayList in Java which have an array as a backing store but otherwise provide lots of methods to run on the objects in the list. contains() and find() are both global methods which can be used for analysing the elements of arrays. – SDJMcHattie Jun 08 '14 at 19:57
  • 8
    It may be worth mentioning that if you are using custom objects, make sure your objects follow [the Equatable protocol](http://nshipster.com/swift-default-protocol-implementations/). – Maxwell Sep 30 '14 at 17:30
3

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")

}
Anit Kumar
  • 8,075
  • 1
  • 24
  • 27
1

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") }