5

I am trying to check if array categories contain number 1 as Int since categories = [Int]() for example categories = {1, 2, 3, 4, 5}

I have tried the below code which gives me error Binary operator '==' cannot be applied to operands of type 'Any' and 'Int'

if categories.contains (where: {$0 == 1}) {
    // 1 is found
}

also tried it without the where and brackets as below which gives me the same error

if categories.contains { $0 == 1 } {
    // 1 is found
}

I tried using just the element as below which gives me error Missing argument label 'where:' in call

if categories.contains(1) {
    // 1 is found
}

How can I do that?

2
  • 1
    How did you declare the categories array? Commented Feb 2, 2018 at 4:22
  • 1
    Your categories declaration it is not a valid syntax Commented Feb 2, 2018 at 4:22

4 Answers 4

6

It seems like your category array is of type Any

Ways to fix it

  • You can declare your array as an Int array

    var categories: [Int]
    

    OR

  • You can change the following piece of code

    if categories.contains { $0 == 1 } {
        // 1 is found
    }
    

    to

    if categories.contains { ($0 as! Int) == 1 } {
        // 1 is found
    }
    

    Note: This method might cause your app to crash if your category array has an element other than of type Int

Sign up to request clarification or add additional context in comments.

2 Comments

What is the meaning of "$0" ? @Malik
@MohamedEzzat $0 is the first parameter passed into a closure. In this instance, when the categories array is traversed with an contains check, $0 holds the value in each iteration
3

it is working See my output in PlayGround

enter image description here

Code used:

var categories : [Int] = [0,1,2,3,4,5,6,7,8,9]

if categories.contains(5)
{
    print("Yes it contains")
}
else
{
    print("it do not")
}

and also This condition is working

if categories.contains (where: {$0 == 1}) {
    print("yes")
}

see your Array Declaration I think there is main Issue

Declaration 1 :

var categories = [Int]()
categories = [0,1,2,3,4,5,6,7,8,9]

Declaration 2 :

var categories : [Int] = [0,1,2,3,4,5,6,7,8,9]

2 Comments

iOS Geek did mention to check the array declaration. The OP stated in the first line that the array is an Int array. I realise that the error states otherwise which tells me that the OP might have declared the array wrongly (exactly like iOS Geek stated). He is not assuming anything my friend.
Yes, @Malik, You are right I had mentioned to check the array declaration For that
3

Regarding error message Binary operator '==' cannot be applied to operands of type 'Any' and 'Int'

Your array is not an Int array instead it contains Any so it needs typecasting before comparision. Declaration of array is also wrong use [] instead of {}. And typecast object as an int ($0 as! Int) == 1 (I'm using force casting here because I know its an Int array).

There are many ways to check if array contains any element.

1> Just try to get the index of element with guard if index is nil means array doesn't contain the element. Although you didn't declare array in right way still I'm considering it a valid array.

let categories: [Int] = [1, 2, 3, 4, 5]
guard categories.index(of: 1) != nil else {
    print("Doesn't Contain")
    return
}
print("Contains")

2> Use contains method

if (categories.contains(1)) {
    print("Contains")
}
else {
    print("Doesn't Contain")
}

3> Not Recommended for this case But still you can get this

let result = categories.filter({$0 == 1})
if result.count == 0 {
   print("Doesn't Contain")
}
else {
   print("Contains")
}

filter returns an array of element which matches with condition. So that if there are multiple 1 in array so it will give you an array of all elements. And $0 describes the object while enumerating the array.

4> Not Recommended for this case

let contains = categories.contains(where: {$0 == 1})
if contains {
    print("Contains")
}
else {
   print("Doesn't Contain")
}

Comments

0

Thanks to your comments made me check the declaration of the array and the problem was that was declared as [Any] after I get it's value from the UserDefaults. I have checked and found the solution on How do I save an Int array in Swift using NSUserDefaults?

// old declaration
let categories = userDefaults.array(forKey:"categories") ?? [Int]()

// new correct declaration
var categories = [Int]()
if let temp = userDefaults.array(forKey:"categories") as? [Int] {
    categories = temp
}

1 Comment

You didn't mention that you are getting your array from UserDefaults. If you mention first line of your code you will get the answer sooner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.