Ahmed Elgendy
Ahmed Elgendy

Reputation: 1700

'[Any?]' is not convertible to 'Anyobject?'

i found this error in this line : if caching === nil on my code:

struct  dataArray {
    var dataById = Dictionary<String, Any>()
}

var id :String?

func dataBegin() {
     let idString = id as String!
     let byCategory = [dataArray().dataById[idString]]

     if byCategory === nil {  //error:'[Any?]' is not convertible to 'Anyobject?'
     // some code
            }
}

Upvotes: 0

Views: 809

Answers (1)

Antonio
Antonio

Reputation: 72760

=== is the identity operator, which is supposed to be used for checking if two references point to the same instance. You probably wanted to use == to check for nil.

However, the byCategory variable is initialized as a non optional array, and as such it cannot be nil, and consequently it cannot be checked for nil.

I think there's a mistake in the logic of that function.

Upvotes: 1

Related Questions