gpt4 book ai didi

ios - 解析 JSON 响应时 : fatal error: array cannot be bridged from Objective-C

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

我收到运行时错误: fatal error :数组无法从 Objective-C 桥接

我的代码如下:

func postCheckUserPhonenumbers(phonenumbers:[String], completionHandler: (([AnyObject?], AnyObject?) -> Void)) {
let urlString = Constant.apiUrl().stringByAppendingFormat(Constant.apiPostCheckUserPhonenumbers)

let phoneNumbersDictionary = phonenumbers.map({ ["number": $0] })

let inputJSON = try? NSJSONSerialization.dataWithJSONObject(phoneNumbersDictionary, options: [])

let request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"
request.HTTPBody = inputJSON

Alamofire.request(request).validate().responseJSON(completionHandler: {response in
if response.result.isSuccess{
if let value = response.result.value {
let json = JSON(value)
let jsonString = json.rawString()
if let users:Array<User> = Mapper<User>().mapArray(jsonString) {
completionHandler(users, nil)
} else {
completionHandler([nil], nil)
}
}
}
if response.result.isFailure{
let message = ApiMessage()
message.message = "No users found"
completionHandler([nil],message)
}
})
}

这就是我的调试器的样子:

enter image description here

最佳答案

明白了:

func postCheckUserPhonenumbers(phonenumbers:[String], completionHandler: (([AnyObject]?, AnyObject?) -> Void)) {
let urlString = Constant.apiUrl().stringByAppendingFormat(Constant.apiPostCheckUserPhonenumbers)

let phoneNumbersDictionary = phonenumbers.map({ ["number": $0] })

let inputJSON = try? NSJSONSerialization.dataWithJSONObject(phoneNumbersDictionary, options: [])

let request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"
request.HTTPBody = inputJSON

Alamofire.request(request).validate().responseJSON(completionHandler: {response in
if response.result.isSuccess{
if let value = response.result.value {
let json = JSON(value)
let jsonString = json.rawString()
if let users:Array<User> = Mapper<User>().mapArray(jsonString) {
completionHandler(users, nil)
} else {
completionHandler(nil, nil)
}
}
}
if response.result.isFailure{
let message = ApiMessage()
message.message = "No users found"
completionHandler(nil,message)
}
})
}

你能看出区别吗?按 Control + F,然后搜索“?”

关于ios - 解析 JSON 响应时 : fatal error: array cannot be bridged from Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39843298/

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