条件绑定的初始化程序必须具有可选类型,而不是“[AnyHashable : Any]”

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

当我这样输入时 :

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

我在这一行 if let data = attributionData

上遇到错误 "Initializer for conditional binding must have Optional type, not '[AnyHashable : Any]'"

如何修复?

func onAppOpenAttribution(_ attributionData: [AnyHashable : Any]?) {

您的 attributionData 应该是可选类型,if let data = attributionData 如果 let 大小写用于安全地解包可选值。但是目前您正在向它传递一个非可选值。所以你有两个选择。将 attributionData 设为可选,或删除 if let statement

选项 1:

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

选项 2:

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