Alamofire 请求回来的数据转nsdic
将Alamofire请求回来的数据转换为NSDictionary可以使用以下方法:
- 使用SwiftyJSON库(推荐):
import Alamofire
import SwiftyJSON
Alamofire.request(url).responseJSON { response in
if let value = response.result.value {
let json = JSON(value)
if let dictionary = json.dictionaryObject as NSDictionary? {
// 转换成功,可以使用dictionary进行操作
}
}
}
- 使用原生的NSJSONSerialization:
import Alamofire
Alamofire.request(url).responseJSON { response in
if let data = response.data {
do {
if let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
// 转换成功,可以使用json进行操作
}
} catch {
// 转换失败,处理错误
}
}
}
以上两种方法都可以将Alamofire请求回来的数据转换为NSDictionary,并可以通过NSDictionary进行进一步的操作
原文地址: https://www.cveoy.top/t/topic/iGxH 著作权归作者所有。请勿转载和采集!