最新消息:VPS服务器又从fzhost.net换回linode了,主题仍用朋友推荐的大前端D8

[已解决]swift中想要把函数作为参数传递结果出错: Value of type ‘(Bool, AnyObject?, JSON) -> Void’ does not conform to expected dictionary value type ‘AnyObject’

Swift crifan 113浏览 0评论
 Value of type ‘(Bool, AnyObject?, JSON) -> Void’ does not conform to expected dictionary value type ‘AnyObject’

搜:

swift function as parameter does not conform to expected dictionary value type ‘AnyObject’

参考:

ios – Swift closure as values in Dictionary – Stack Overflow

ios – Argument type ‘[String?]’ does not conform to expected type ‘AnyObject’ – Stack Overflow

Swift Literal Convertibles – NSHipster

9 Essential Swift Interview Questions and Answers | Toptal

generics – Swift: Argument type ‘[String:ValueType]’ does not conform to expected type ‘AnyObject’ – Stack Overflow

swift function as dictionary value

Swift Dictionaries

最后,暂时是可以凑合成:

    func deleteUserFromGroup(userIdToDelete:String, deleteDoneHandler: ((String) -> Void)) {
                enum DataType {
                    case AsString(String)
                    case AsClosure((String) -> Void)
                }
               
//                var dict:Dictionary<String,DataType> = [
//                    "string":DataType.AsString("value"),
//                    "closure":DataType.AsClosure({(argument:String) -> Void in
//                        return "value"
//                        }
//                    )
//                ]
               
               
//                let FuncAsParaDict:[String: (String) -> Void] = [
//                    "deleteDoneHandler" : deleteDoneHandler]
               
//                let extraParas:[String:AnyObject] = [
//                let extraParas:[String:NSObject] = [
                let extraParas:[String:DataType] = [
//                    "httpVerb" : String(HTTPVerb.DELETE),
                    "httpVerb" : DataType.AsString(String(HTTPVerb.DELETE)),
//                    "FuncAsParaDict" : FuncAsParaDict
//                    "deleteDoneHandler" : deleteDoneHandler
                    "deleteDoneHandler" : DataType.AsClosure(deleteDoneHandler)
                ]
                print("extraParas=\(extraParas)")
               
                getUrlRespJsonDict_async(deleteFromGroupUrl, extraParas: extraParas, respJsonDictHandler: deleteFromGroupHandler)
}

了,但是此处不想去修改之前已有函数 

getUrlRespJsonDict_async

中[String:AnyObject]的类型的extraParas了,所以还是懒得继续折腾了。

[总结]

此处,想要一个函数function(==closure==block)作为一个字典dictionary变量的值value的话,可以这么写:

func deleteUserFromGroup(userIdToDelete:String, deleteDoneHandler: ((String) -> Void)) {
                enum DataType {
                    case AsString(String)
                    case AsClosure((String) -> Void)
                }
                let extraParas:[String:DataType] = [
                    "httpVerb" : DataType.AsString(“DELETE"),
                    "deleteDoneHandler" : DataType.AsClosure(deleteDoneHandler)
                ]
                print("extraParas=\(extraParas)")
}

即可。

转载请注明:在路上 » [已解决]swift中想要把函数作为参数传递结果出错: Value of type ‘(Bool, AnyObject?, JSON) -> Void’ does not conform to expected dictionary value type ‘AnyObject’

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
21 queries in 0.232 seconds, using 9.78MB memory