无法转换类型' [String:Any]的值?'预期的论证类型' _?'

时间:2017-08-17 13:59:58

标签: ios swift

P和R是通用类型

open func route<P,R>(url:URL , param : P? ) -> R?{
    guard let s = url.scheme ,let h = url.host  else{
        #if DEBUG
            print("url error")
        #endif
        return nil
    }

    let urlString = "\(s)://\(h)\(url.path)"


    guard let t = self.urlTaskMap[urlString] as? RoyTaskClosure<P,R> else {

        print("closure unmatched,url -> \(urlString)")

        return nil
    }

    let returnValue = t(param)

    return returnValue
}

当我调用像

这样的函数时
public func viewController(url:URL,param:[String:Any]?) -> RoyProtocol?{
    if let vc = self.route(url: url, param: param){
        return vc
    }
    return nil
}

Xcode向我显示错误消息

Cannot convert value of type '[String:Any]?' to expected argument type '_?'

天啊!有人能告诉我如何解决它吗?

1 个答案:

答案 0 :(得分:0)

我得到了解决方案。用这个

替换你的方法
public func viewController(url:URL,param:[String:Any]?) -> RoyProtocol?{
    let vc: RoyProtocol? = self.route(url: url, param: param)
    return vc
}