![](/img/trans.png)
[英]Nil Coalescing returning nil instead of default value when parsing JSON
[英]how to send nil when there is no value for a field in json?
email和a_code是可選的。 我確實想在空的時候發送nil。 我應該檢查它們是否為nil,將它們從json中刪除嗎?
func updateProfile(fullName: String,address: String,homePhone: String,email: String, a_code: String, completion : @escaping CompletionHandler) {
let body : [String : Any] = [
"name": fullName,
"address": address,
"h_phone": homePhone,
"email": email,
"a_code": a_code
]
Alamofire.request(UPDATE_PROFILE_FIRST, method: .post, parameters: body, encoding: JSONEncoding.default, headers: AUTH_HEADER).validate().responseJSON { (response) in
switch response.result {
case .success(let value):
let json = JSON(value)
print(json)
let s = json["s"].intValue
if s == 24 {
completion(true)
}else {
completion(false)
}
case .failure(let error):
print(error.localizedDescription)
completion(false)
}
}
}
func updateProfile(fullName: String,address: String,homePhone: String,email: String, a_code: String, completion : @escaping CompletionHandler) {
let body : [String : Any] = [
"name": fullName,
"address": address,
"h_phone": homePhone,
"email": email.isEmpty ? nil : email ,
"a_code": a_code.isEmpty ? nil : a_code
]
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.