将NSObject转换为NSDictionary JSON swift 2.0

时间:2015-12-21 14:25:20

标签: ios json swift swift2

我尝试在里面用JSON发出接收通知。所以我在AppDelegate.swift里面编码。 我将NSObject转换为NSDictionary时遇到了问题。

这是我的代码:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        PFPush.handlePush(userInfo)
        if application.applicationState == UIApplicationState.Active {

            PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
            let msg = userInfo["test"] as? NSObject
            print(userInfo)

            if msg == nil{

                    let dict = msg as! Dictionary<String, Dictionary<String, Array<Dictionary<String, AnyObject>>>>
                    let tests = dict["test"]!["test"]!
                    for test in tets {
                        let header : AnyObject? = test["message"]
                        let body : AnyObject? = test["aps"]
                        let url : AnyObject? = test["link"]

                        print("test ID: \(header)")
                        print("test Name: \(body)")
                        print("test Needs Password: \(url)")


                }
            }
        }

有2个问题: 1.我已经收到JSON数据作为对象 让msg = userInfo [&#34; test&#34;]为? NSObject的

Printing description of userInfo:
    ▿ 3 elements
      ▿ [0] : 2 elements
        - .0 : message
        - .1 : test iOS
      ▿ [1] : 2 elements
        - .0 : aps
        - .1 : 0 elements
      ▿ [2] : 2 elements
        - .0 : link
        - .1 :

但我的消息是零。所以如果msg == nil

,我必须继续执行此代码
  1. 我不知道如何使用NSJSONSerialization,因为错误总是显示&#34;无法将NSObjet转换为NSData类型&#34;

0 个答案:

没有答案