Ios alamofire swift3的故障响应
你好,我被阿拉莫菲尔绊倒了Ios alamofire swift3的故障响应,ios,swift,dictionary,swift3,alamofire,Ios,Swift,Dictionary,Swift3,Alamofire,你好,我被阿拉莫菲尔绊倒了 let keys = [CNContactPhoneNumbersKey, CNContactGivenNameKey] let request = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor]) let contactStore = CNContactStore() do { try contactStore.enumerateContacts
let keys = [CNContactPhoneNumbersKey, CNContactGivenNameKey]
let request = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])
let contactStore = CNContactStore()
do {
try contactStore.enumerateContacts(with: request) {
(contact, stop) in
// Array containing all unified contacts from everywhere
self.contacts.append(contact)
}
}
catch {
print("unable to fetch contacts")
}
var contactArray = [[String:String]]()
for i in 0..<contacts.count{
var mobiles = ""
for num in contacts[i].phoneNumbers {
mobiles = num.value.stringValue
}
var theDict = ["contact_id": "\(i)", "full_name": contacts[i].givenName, "mobile_number": "\(mobiles)"]
contactArray.append(theDict)
}
dictParams["contacts"] = dictContacts
let theParams = ["contacts":contactArray] as [String:AnyObject]
print("dict theParams: \(theParams)")
阿拉莫菲尔的回应是:
this is the response = retrieve FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 1." UserInfo={NSDebugDescription=Invalid value around character 1.}))
但是如果我改变参数,只有这样的字符串
[“联系人”:“测试联系人”]
阿拉莫菲尔反应成功了,它的工作
请帮助我:)谢谢使用
responseString
而不是responseJSON
。然后使用JSONSerialization
将响应转换为JSON比如:
JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
这不是阿拉莫菲尔的问题。api需要“contacts”参数作为
String
,并且您正在发送字典的数组。如果要在“contacts”参数中发送数组
数据,请让api开发人员更改参数类型,或在可能的情况下将字符串作为“contacts”参数中的数据发送
问题是:
这是response=retrieve FAILURE:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误域=NSCOCAerorDomain代码=3840“字符1周围的无效值”。“用户信息={NSDebugDescription=字符1周围的无效值”。)
也仅声明无效字符。我的参数如下
// .toJson to your array object or dictonary object and try out
let parameters: [String : Any] = [
"products":[
["pid":"72","qnty":"1"],
["pid":"4","qnty":"1"],
["pid":"3","qnty":"1"]
].toJson ?? "[]",
]
使用此扩展将数组对象转换为JSON字符串
extension Array where Element: Codable {
public var toData: Data {
let encoder = JSONEncoder()
do {
return try encoder.encode(self)
}
catch {
fatalError(error.localizedDescription)
}
}
public var toJson: String? {
return toData.toJson
}
}
如何将responseString转换为responseJSON?我已经在使用responseString,并且响应成功。我应该将什么转换为dictionary?答复。?
// .toJson to your array object or dictonary object and try out
let parameters: [String : Any] = [
"products":[
["pid":"72","qnty":"1"],
["pid":"4","qnty":"1"],
["pid":"3","qnty":"1"]
].toJson ?? "[]",
]
extension Array where Element: Codable {
public var toData: Data {
let encoder = JSONEncoder()
do {
return try encoder.encode(self)
}
catch {
fatalError(error.localizedDescription)
}
}
public var toJson: String? {
return toData.toJson
}
}