Initializer for conditional binding must have Optional type, not '[AnyHashable : Any]'

802 Views Asked by At

when I type like this :

func onAppOpenAttribution(_ attributionData: [AnyHashable : Any]!) {
    if let data = attributionData {
        if let link = data["link"]{
            print("link:  \(link)")
        }
    }
}

I got error "Initializer for conditional binding must have Optional type, not '[AnyHashable : Any]'" on this line if let data = attributionData

How to repair that?

1

There are 1 best solutions below

3
Keshu R. On BEST ANSWER
func onAppOpenAttribution(_ attributionData: [AnyHashable : Any]?) {

your attributionData should be optional type, if let data = attributionData if let case is used to safely unwrap an optional value. But currently you are passing a non optional value to it. So you have two options. Either to make attributionData as optional, or remove if let statement

Option 1:

func onAppOpenAttribution(_ attributionData: [AnyHashable : Any]?) {
    if let data = attributionData {
        if let link = data["link"]{
            print("link:  \(link)")
        }
    }
}

Option 2:

func onAppOpenAttribution(_ attributionData: [AnyHashable : Any]) {
    let data = attributionData 
    if let link = data["link"]{
       print("link:  \(link)")
     }
  }
}