无法在Swift中将'__NSSingleObjectArrayI'类型的值转换为'NSDictionary'

时间:2017-04-17 12:37:24

标签: ios swift nsdictionary

当我解析Api时,我得到了响应并存储了对Dictionary的完整响应然后我将其拆分为数组和字典,但是我收到错误所以请帮助解决错误,

在这里,我给出了我尝试过的代码。

let responseDictionary:NSDictionary = responseObject as! NSDictionary
        print(responseDictionary)
        if let statusCode:NSNumber = responseDictionary.value(forKey: "success") as? NSNumber{
            if statusCode == 1{
                print("statusCode :",statusCode)

                let incomingRequetArray:NSArray = responseDictionary.value(forKey: "incoming_requests") as! NSArray
                print("incomingRequetArray",incomingRequetArray)

                if (incomingRequetArray.count > 0)
                {
                    let requestDataDict:NSDictionary = incomingRequetArray.value(forKey: "request_data") as! NSDictionary  -----> Error in this line
                    print("requestDataDict",requestDataDict)

                    let ownerArray:NSArray = requestDataDict.value(forKey: "owner") as! NSArray
                    print("ownerArray",ownerArray)
                    self.mutDictOwner = ownerArray.object(at: 0) as! NSMutableDictionary

                }
            }

responseDictionary是

"incoming_requests" =     (
            {
        datetime = "2017-04-17 12:25:58";
        later = 0;
        "request_data" =             {
            dog =                 (
            );
            owner =                 {
                address = "";
                "d_latitude" = "0.00000000";
                "d_longitude" = "0.00000000";
                "dest_latitude" = "0.00000000";
                "dest_longitude" = "0.00000000";
                latitude = "11.01449160";
                longitude = "76.98273220";
                name = "Sss Sas";
                "num_rating" = 0;
                "payment_type" = 1;
                phone = "+919123456789";
                picture = "";
                rating = "0.00";
            };
            "payment_mode" = 1;
        };
        "request_id" = 1474;
        "request_services" = 30;
        "time_left_to_respond" = 21;
    }
);
"is_approved" = 1;
"is_approved_txt" = Approved;
"is_available" = 1;
success = 1;

4 个答案:

答案 0 :(得分:3)

这是因为你的“request_data”是一个Object而不是一个Dictionary,看看“incoming_request”是如何启动的(“(”)和“request_data”以“{”开头。你可以做的是访问此

let requestDataDict:NSDictionary = incomingRequetArray.value(forKey: "request_data") as! [AnyObject]-----> Error in this line
                print("requestDataDict",requestDataDict)
                let x = requestDataDict[0] as! NSDictionary
                let ownerArray:NSArray = x.value(forKey: "owner")as! NSArray

答案 1 :(得分:1)

在发生错误的行中,尝试将incomingRequetArray.value...更改为incomingRequetArray[0].value...,因为您需要访问数组中包含的内部字典,然后查询给定密钥的值。

答案 2 :(得分:1)

你做错了incomingRequetArray是NSArray,你获得了关键的价值

写这个

let requestDataDict:NSDictionary = incomingRequetArray[0]as!NSDictionary
let newDict: NSDictionary = requestDataDict.object(forKey: "request_data")
print("newDict is ---%@", newDict)

您使用了NSDictionary和NSArray格式所以我根据您编写代码。但是在swift中的进一步使用不要使用NSDictionary和NSArray。

答案 3 :(得分:1)

这是一个合理的Swift 转录,可以解决错误。为方便起见,它使用类型别名。

typealias JSONDictionary = [String:Any]

let responseDictionary = responseObject as! JSONDictionary
print(responseDictionary)
if let success = responseDictionary["success"] as? Int, success == 1 {
    print("statusCode :", success)

    let incomingRequestArray = responseDictionary["incoming_requests" as! [JSONDictionary]
    print("incomingRequetArray",incomingRequestArray)

    if let requestDataDict = incomingRequestArray.first {
        print("requestDataDict",requestDataDict)

        if let ownerArray = requestDataDict["owner"] as? [JSONDictionary], !ownerArray.isEmpty {
        print("ownerArray",ownerArray)
        self.mutDictOwner = ownerArray[0] as! JSONDictionary
    }
}