当iOS应用触发名为“ addtocart”的API事件时,该应用崩溃并返回以下错误。
url https://example.com/index.php/?route=api/cart/addToCart参数 [“ product_id”:“ 12098”,“ option”:“ {\ n \ n}”,“ quantity”:“ 0”,“ token”: “” 2f42lqjie21cbzr1r0gsgphqtl“,”宽度“:” 750.000000“] { 错误= 1; 讯息=( “需要颜色!” ); }无法将类型'__NSSingleObjectArrayI'(0x1b5bee3b0)的值强制转换为'NSString'(0x1b5bfaad8)。 2018-08-02 19:50:28.493678 + 0300 Ishtari [35479:3846514]无法转换为 将'__NSSingleObjectArrayI'(0x1b5bee3b0)键入为'NSString' (0x1b5bfaad8)。警告:无法执行支持代码以进行读取 过程中的Objective-C类数据。这可能会降低质量 类型信息可用。 (lldb)
该产品具有称为颜色的选项,因此在添加到购物车之前,用户应选择要检查商品的颜色。该应用程序返回选项名称“颜色”,但没有给我提供要选择的值(“选项”:“ {\ 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)
}
请帮忙!
答案 0 :(得分: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)!