Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Expression implicitly coerced from 'Any??' to 'Any?'

Tags:

swift

I have this code in Swift:

 var dictionary: [String: Any?]? 

 fileprivate func value(forKey key: Key) -> Any? {
    if let dictionary = self.dictionary {
        return dictionary[key.rawValue]
    }

     ...
 }

I get a warning "Expression implicitly coerced from 'Any??' to 'Any?' in the return statement ". What am I doing wrong?

like image 987
Deepak Sharma Avatar asked Dec 13 '25 08:12

Deepak Sharma


1 Answers

The value of your dictionary is Any?. The result of accessing a dictionary value is an optional because the key might not exist. So you end up with Any??.

There's really no need for your dictionary to be declared to have optional values.

Change it to [String: Any]? and your issue goes away.

like image 170
rmaddy Avatar answered Dec 16 '25 17:12

rmaddy