由于对成员 jsonObject 的引用不明确,我无法编译我的 alamofire 代码。为什么?
I cannot compile my alamofire code due to ambiguous reference to member jsonObject. Why?
我在 Swift
中有一个代码,我在其中通过 Alamofire
调用我的后端代码:
Alamofire.request(url, method: .post, parameters: (parameters), encoding: JSONEncoding.default)
.validate()
.responseJSON { response in
switch response.result {
case .success:
do {
if let jsonData = try JSONSerialization.jsonObject(with: response.result.value, options:.allowFragments) as? [String:Any] {
for requestJSON in jsonData["geojson"]["features"] {
if let request = SingleCluster.fromJSON(JSON(requestJSON)){
let pinOne = CustomCluster()
pinOne.coordinate = CLLocationCoordinate2D(latitude: request.latitude, longitude: request.longitude)
pinOne.amount = request.amount
self.eventsArray.append(pinOne);
}
}
}
} catch let err{
print(err.localizedDescription)
}
completionHandler!()
case .failure(let error):
print("error")
}
}
问题是这一行:
if let jsonData = try JSONSerialization.jsonObject(with: response.result.value, options:.allowFragments) as? [String:Any] {
出现错误:
Ambiguous reference to member 'jsonObject(with:options:)'
我试图将其转换为 [String:Any]
或 [String:AnyObject]
但它没有任何改变。我在这里做错了什么?
更改此行:
if let jsonData = try JSONSerialization.jsonObject(with: response.result.value, options:.allowFragments) as? [String:Any] {
收件人:
if let data = response.data, let jsonData = try JSONSerialization.jsonObject(with: data, options:.allowFragments) as? [String:Any] {
你已经使用了responseJSON
和Alamofire
所以你会在completion block中得到Serialized JSON,不需要使用JSONSerialization
直接使用它。
switch response.result {
case .success:
if let jsonData = JSON(response.result.value) {
for requestJSON in jsonData["geojson"]["features"] {
if let request = SingleCluster.fromJSON(requestJSON){
let pinOne = CustomCluster()
pinOne.coordinate = CLLocationCoordinate2D(latitude: request.latitude, longitude: request.longitude)
pinOne.amount = request.amount
self.eventsArray.append(pinOne);
}
}
}
case .failure(let error):
print("error")
}
我在 Swift
中有一个代码,我在其中通过 Alamofire
调用我的后端代码:
Alamofire.request(url, method: .post, parameters: (parameters), encoding: JSONEncoding.default)
.validate()
.responseJSON { response in
switch response.result {
case .success:
do {
if let jsonData = try JSONSerialization.jsonObject(with: response.result.value, options:.allowFragments) as? [String:Any] {
for requestJSON in jsonData["geojson"]["features"] {
if let request = SingleCluster.fromJSON(JSON(requestJSON)){
let pinOne = CustomCluster()
pinOne.coordinate = CLLocationCoordinate2D(latitude: request.latitude, longitude: request.longitude)
pinOne.amount = request.amount
self.eventsArray.append(pinOne);
}
}
}
} catch let err{
print(err.localizedDescription)
}
completionHandler!()
case .failure(let error):
print("error")
}
}
问题是这一行:
if let jsonData = try JSONSerialization.jsonObject(with: response.result.value, options:.allowFragments) as? [String:Any] {
出现错误:
Ambiguous reference to member 'jsonObject(with:options:)'
我试图将其转换为 [String:Any]
或 [String:AnyObject]
但它没有任何改变。我在这里做错了什么?
更改此行:
if let jsonData = try JSONSerialization.jsonObject(with: response.result.value, options:.allowFragments) as? [String:Any] {
收件人:
if let data = response.data, let jsonData = try JSONSerialization.jsonObject(with: data, options:.allowFragments) as? [String:Any] {
你已经使用了responseJSON
和Alamofire
所以你会在completion block中得到Serialized JSON,不需要使用JSONSerialization
直接使用它。
switch response.result {
case .success:
if let jsonData = JSON(response.result.value) {
for requestJSON in jsonData["geojson"]["features"] {
if let request = SingleCluster.fromJSON(requestJSON){
let pinOne = CustomCluster()
pinOne.coordinate = CLLocationCoordinate2D(latitude: request.latitude, longitude: request.longitude)
pinOne.amount = request.amount
self.eventsArray.append(pinOne);
}
}
}
case .failure(let error):
print("error")
}