gpt4 book ai didi

ios - 使用 Alamofire 解析 json 时出现 AppDelegate 错误

转载 作者:行者123 更新时间:2023-11-29 11:46:10 24 4
gpt4 key购买 nike

从下面的代码来看,它没有显示任何错误,但它会出现运行时 appDelegate 错误,其原因是 Terminating app due to uncaught exception 'NSInvalidArgumentException'。请告诉我我想做什么来摆脱这个......

var urlstring: String!

urlstring = "\(signInAPIUrl)rooms/room_type"
urlstring = urlstring.replacingOccurrences(of: "Optional(", with: "")
urlstring = urlstring.replacingOccurrences(of: ")", with: "")
urlstring = urlstring.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)!

print(urlstring)
self.callSiginGBAPI(url: "\(urlstring!)")
}
func callSiginGBAPI(url : String){

print("url: \(url)")

Alamofire.request(url).responseJSON { (response) in

self.parseDataGB(JSONData: response.data!)

print("Response:\(response)")

}

}
func parseDataGB(JSONData : Data){

do{

let readableJSon = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! jsonSTD

print(" !!! \(readableJSon[0])")

let value = readableJSon[0] as AnyObject

if let final = value.object(forKey: "id")
{
print(final)

let first_name:String = value.object(forKey: "id") as! String
let last_name:String = value.object(forKey: "type") as! String
let list_type:String = value.object(forKey: "list_type") as! String
print(first_name)
print(last_name)
print(list_type)

} else{
}
}
catch{

print(error)

}

}

最佳答案

使用以下扩展将数据转换为 JSON 对象:

extension Data {
func JSONObject() -> AnyObject? {
do {
let content = try JSONSerialization.jsonObject(with: self as Data, options: JSONSerialization.ReadingOptions.allowFragments)
return content as AnyObject?
} catch _ as NSError {
return nil
}
}

var string: String {
return String(data: self as Data, encoding: String.Encoding.utf8) ?? "Error: Not able to get string from the data."
}

}

回应

let info = response.data?.JSONObject() 

关于ios - 使用 Alamofire 解析 json 时出现 AppDelegate 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43758691/

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