gpt4 book ai didi

ios - 无法将类型 '__NSSingleObjectArrayI' 的值转换为 'NSString'

转载 作者:行者123 更新时间:2023-11-30 11:16:54 24 4
gpt4 key购买 nike

当 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)

该产品有一个名为“颜色”的选项,因此在添加到购物车之前,用户应该选择要结帐的商品的颜色。该应用程序返回选项名称“Color”,但没有给我可供选择的值(“option”:“{\n\n}”),这导致应用程序崩溃,在调用HTTPApi时出现上述错误。

   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)
}

请帮忙!

最佳答案

错误很明显:

Could not cast value of type '__NSSingleObjectArrayI' (0x1b5bee3b0) to '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 原生类型和 key 订阅

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)!

关于ios - 无法将类型 '__NSSingleObjectArrayI' 的值转换为 'NSString',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51658847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com