本文介绍了如何在Swift 3中解析JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Socket中获取了一些我想访问的数据,但是却收到错误消息,提示每次都无法将NSArray转换为NSDictionary.

I am getting some data from Socket which I would like to access but I am getting error saying can't convert NSArray to NSDictionary each time.

struct SocketEventHandler {
    let event: String
    let id: UUID
    let callback: NormalCallback

    func executeCallback(with items: [Any], withAck ack: Int, withSocket socket: SocketIOClient) {
        callback(items, SocketAckEmitter(socket: socket, ackNum: ack))
    }
}


let onFriendRequestStatus: NormalCallback = {dataArray,socketAck in
        AppLog.debug(tag: TAG, msg: "Response: \(dataArray)")
        AppLog.debug(tag: TAG, msg: "Response: \(dataArray[0])")
        let response  = dataArray[0] as! Dictionary<String, Any> **// APP CRASHES ON THIS LINE**
}

响应:-

[
    {
        "status": "A",
        "acceptorId": 126,
        "id": 94,
        "createdAt": "2017-06-05T09:50:42.000Z",
        "updatedAt": "2017-06-05T09:52:26.000Z",
        "userId": 126,
        "friendId": 401
    }
]

错误-无法将类型'__NSArrayM'(0x102f0de00)的值强制转换为'NSDictionary'(0x102f0e2d8).

Error - Could not cast value of type '__NSArrayM' (0x102f0de00) to 'NSDictionary' (0x102f0e2d8).

推荐答案

在句子崩溃时设置断点,然后使用以下命令检查dataArray的确切含义:

Make a breakpoint on sentence crashes, and then use following command to check what exactly the dataArray is:

po print(dataArray)

这篇关于如何在Swift 3中解析JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:09