最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[部分解决]swift中给回调函数添加参数

Swift crifan 2559浏览 0评论

代码:

                       let actionsAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.Alert)
                        let cancelAlertAction = UIAlertAction(title: "复制", style: UIAlertActionStyle.Default, handler: copyHandler)
                        actionsAlertController.addAction(cancelAlertAction)
                        let sureAlertAction = UIAlertAction(title: "搜藏", style: UIAlertActionStyle.Default, handler: addFavoriteHandler)
                        actionsAlertController.addAction(sureAlertAction)
                        self.presentViewController(actionsAlertController, animated: true, completion: nil)
    func copyHandler(alerAction:UIAlertAction){
        print("copyHandler alerAction=\(alerAction)")
    }
   
    func addFavoriteHandler(alerAction:UIAlertAction){
        print("addFavoriteHandler alerAction=\(alerAction)")
       
        /*
        PUT /user/{currentuser}/pins/{resourceId}
        收藏资源
        返回string(resurceId)
        */
       
        let curResId:String = ""
       
        let addToFavoriteUrl:String = StrServerUrl + "/user/" + gCurUserItem.id + "/pins/" + curResId
       
    }

想要给:copyHandler和addFavoriteHandler添加额外的参数。

swift handler add parameter

scenekit – completion handler with parameters (Swift) – Stack Overflow

ios – Completion Handler in Swift – Stack Overflow

Swift Function Currying | Xebia Blog

好像自己找到了:

然后:

结果不论是哪种:

                        favoriteAlertAction.setValue(curMessageTVC.message, forKey: : "curMessage")
                        favoriteAlertAction.setValue(curMessageTVC.message, forUndefinedKey: "curMessage")

都会出错:

Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[<UIAlertAction 0x7ff18dae81d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key curMessage.’

搜:

swift UIAlertAction add parameter

最后还是没有解决。

暂时用了,额外用全局变量,解决UIAlertAction的callback的handler中的参数传递。。。

代码如下:

                        let actionsAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.Alert)
                        let cancelAlertAction = UIAlertAction(title: "复制", style: UIAlertActionStyle.Default, handler: copyActionHandler)
                        actionsAlertController.addAction(cancelAlertAction)
                        let favoriteAlertAction = UIAlertAction(title: "收藏", style: UIAlertActionStyle.Default, handler: addFavoriteActionHandler)
                        actionsAlertController.addAction(favoriteAlertAction)
                        self.presentViewController(actionsAlertController, animated: true, completion: nil)
    func addFavoriteActionHandler(alerAction:UIAlertAction){
        print("addFavoriteActionHandler alerAction=\(alerAction)")
        /*
        PUT /user/{currentuser}/pins/{resourceId}
        收藏资源
        返回string(resurceId)
        */
       
        if longPressedMessageTVC != nil {
            let curMessage:Message = longPressedMessageTVC!.message
            let curResId:String = curMessage.id
            print("curResId=\(curResId)") //message-813369d4-905b-40be-9f02-8e1641479f75
           
            let addToFavoriteUrl:String = StrServerUrl + "/user/" + gCurUserItem.id + "/pins/" + curResId
           
            let extraParas:[String:AnyObject] = [
                "httpVerb" : String(HTTPVerb.PUT),
                "curResId"  : curResId
            ]
            print("extraParas=\(extraParas)")
            getUrlRespJsonDict_async(addToFavoriteUrl, extraParas: extraParas, respJsonDictHandler: addToFavoriteHandler)
        }
    }

转载请注明:在路上 » [部分解决]swift中给回调函数添加参数

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.200 seconds, using 22.16MB memory