无法将“__NSSingleObjectArrayI”类型的值转换为“NSString”

问题描述 投票:0回答:1

当 iOS 应用程序触发名为“addtocart”的 API 事件时,应用程序崩溃并返回以下错误。

url https://example.com/index.php/?route=api/cart/addToCart params
["product_id": "12098", "option": "{\n\n}", "quantity": "0", "token":
"2f42lqjie21cbzr1r0gsgphqtl", "width": "750.000000"] {
    error = 1;
    message =     (
        "Color required!"
    ); } Could not cast value of type '__NSSingleObjectArrayI' (0x1b5bee3b0) to 'NSString' (0x1b5bfaad8). 2018-08-02
19:50:28.493678+0300 Ishtari[35479:3846514] Could not cast value of
type '__NSSingleObjectArrayI' (0x1b5bee3b0) to 'NSString'
(0x1b5bfaad8). warning: could not execute support code to read
Objective-C class data in the process. This may reduce the quality of
type information available. (lldb)

该产品有称为颜色的选项,因此在添加到购物车之前,用户应该选择要结帐的商品的颜色。该应用程序返回选项名称“颜色”,但没有给我可供选择的值(

"option": "{\n\n}"
),这导致应用程序崩溃,并在
callingHTTPApi
时出现上述错误。

   func callingHttppApi(){
        DispatchQueue.main.async{
            NetworkManager.sharedInstance.showLoader()
            let sessionId = self.defaults.object(forKey:"ic_token");
            let width = String(format:"%f", SCREEN_WIDTH * UIScreen.main.scale)
            self.view.isUserInteractionEnabled = false
            
            if self.whichApiToProcess == "addtocart"{
                var requstParams = [String:String]();
                requstParams["width"] = width
                requstParams["product_id"] = self.productId
                requstParams["ic_token"] = sessionId as? String
                requstParams["quantity"] = self.quantityValue.text
                do {
                    let jsonSortData =  try JSONSerialization.data(withJSONObject: self.optionDictionary, options: .prettyPrinted)
                    let jsonSortString:String = NSString(data: jsonSortData, encoding: String.Encoding.utf8.rawValue)! as String
                    requstParams["option"] = jsonSortString
                }
                catch {
                    print(error.localizedDescription)
                }
                
                
                NetworkManager.sharedInstance.callingHttpRequest(params:requstParams, apiname:"cart/addToCart", cuurentView: self){success,responseObject in
                    if success == 1{
                        let dict = responseObject as! NSDictionary;
                        NetworkManager.sharedInstance.dismissLoader()
                        if dict.object(forKey: "fault") != nil{
                            let fault = dict.object(forKey: "fault") as! Bool;
                            if fault == true{
                                self.loginRequest()
                            }
                        }else{
                            print(dict)
                            self.view.isUserInteractionEnabled = true
                            let dict = responseObject as! NSDictionary
                            if dict.object(forKey: "error") as! Int == 0{
                                NetworkManager.sharedInstance.showSuccessSnackBar(msg: dict.object(forKey: "message") as! String)
                                let data = dict.object(forKey: "total") as! String
                                self.tabBarController!.tabBar.items?[3].badgeValue = data.components(separatedBy: " ")[0]
                                self.navigationCart(cartCount:data.components(separatedBy: " ")[0])
                                if self.goToBagFlag == true{
                                    self.tabBarController!.selectedIndex = 3
                                }
                                
                            }else{
                                NetworkManager.sharedInstance.showWarningSnackBar(msg: dict.object(forKey: "message") as! String)
                            }
                        }
                    }else if success == 2{
                        NetworkManager.sharedInstance.dismissLoader()
                        self.callingHttppApi()
                    }
                }
            }

当它崩溃时,控制台将我定向到这一行

                        }else{
                            NetworkManager.sharedInstance.showWarningSnackBar(msg: dict.object(forKey: "message") as! String)
                        }

请帮忙!

ios swift swift3 nsstring
1个回答
0
投票

错误很明显:

无法将类型“__NSSingleObjectArrayI”(0x1b5bee3b0)的值转换为“NSString”(0x1b5bfaad8)

左侧显示实际类型(

Array
),右侧是你做错的地方(
String
)

NetworkManager.sharedInstance.showWarningSnackBar(msg: dict.object(forKey: "message") as! [String])

如果

msg
应该是
String
join
字符串

let messageArray = dict.object(forKey: "message") as! [String] 
NetworkManager.sharedInstance.showWarningSnackBar(msg: messageArray.joined(separator: ", "))

顺便使用 Swift 原生类型和密钥订阅

let dict = responseObject as! [String:Any]
...
NetworkManager.sharedInstance.showWarningSnackBar(msg: dict["message"] as! [String])

而且 - 抱歉 - 这个语法太可怕了:

let jsonSortString:String = NSString(data: jsonSortData, encoding: String.Encoding.utf8.rawValue)! as String

请更换为

let jsonSortString = String(data: jsonSortData, encoding: .utf8)!
© www.soinside.com 2019 - 2024. All rights reserved.