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

[已解决]swift中把 NSDictionary转换为JSON

JSON crifan 2354浏览 0评论

已有代码:

    func handleRemoteNotification(remoteInfo:NSDictionary) {
        print("handleRemoteNotification remoteInfo=\(remoteInfo)")
       
        if UIApplication.sharedApplication().applicationState == UIApplicationState.Active {
            print("not handle remote notification for app is active running")
        } else {
            print("applicationState=\(UIApplication.sharedApplication().applicationState)")
           
            let messageDict = JSON(remoteInfo)
            SingletonConversationTVC().processSingleMessageDict(messageDict)
        }
    }

中,想要把NSDictionary类型的remoteInfo,转换为(SwiftyJSON的)JSON

搜:

swift NSDictionary to SwiftyJSON

SwiftyJSON/README.md at master · SwiftyJSON/SwiftyJSON · GitHub

SwiftyJSON – code4swift|swift迷

swift – NSDictionary > JSON String using SwiftyJSON – Stack Overflow

好像是可以像上述代码中直接传入NSDictionary??

是的,代码中直接就可以了。

改为:

    func handleRemoteNotification(remoteNotification:NSNotification) {
        print("handleRemoteNotification remoteNotification=\(remoteNotification)")
        if let userInfoDict = remoteNotification.object {
            print("userInfoDict=\(userInfoDict)")
           
            if UIApplication.sharedApplication().applicationState == UIApplicationState.Active {
                print("not handle remote notification for app is active running")
            } else {
                print("applicationState=\(UIApplication.sharedApplication().applicationState)")
                SingletonConversationTVC().updateAllConversation()
            }
        }
    }

即可。

转载请注明:在路上 » [已解决]swift中把 NSDictionary转换为JSON

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.187 seconds, using 22.00MB memory