为什么我不能使用 AlamoFire 和 SwiftyJson 在 swift 中创建我的 class 的对象?
Why I can't create objects of my class in swift using AlamoFire and SwiftyJson?
我有一个函数可以从 swift 中的 json 节点创建对象:
class func fromJSON(json: JSON) -> SingleRequest? {
var title: String
if let titleOrNil = json["title"].string {
title = titleOrNil
} else {
title = ""
}
let locationName = json["location"].string
let discipline = json["discipline"].string
let lat = json["location"]["coordinates"][1].doubleValue
let lon = json["location"]["coordinates"][0].doubleValue
let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
return SingleRequest(title: title, locationName: locationName!, discipline: discipline!, coordinate: coordinate)
}
现在,我正在尝试使用 alamofire
和 swiftyJson
从我的网络服务中获取所有数据并创建 SingleRequest
对象。我这样做如下所示:
func fetchRequests(radius: Double, lat: Double, lon: Double){
Alamofire.request(.GET, "https://mywebservice")
.responseJSON { response in
switch response.result {
case .Success:
if let jsonData = response.result.value {
for requestJSON in jsonData {
if let request = SingleRequest.fromJSON(requestJSON){
//do sth with a single request here
//e.g. print(request.discipline)
}
}
}
case .Failure(let error):
print("SWITCH ERROR")
print(error)
}
}
}
但我收到一个错误:
所以我的问题是 - 如何使用 alamoFire
和 SwiftyJson
创建自定义 SingleRequest
?
诚然,我不知道 "type" response.result.value 是什么,但如果它可以转换为可以迭代的东西,那么这里有一个例子:
if let jsonData = response.result.value as? [(some type that can be iterated)] {
// do something with jsonData ...
}
你的问题在这里:
if let jsonData = response.result.value {
for requestJSON in jsonData {
if let request = SingleRequest.fromJSON(JSON(requestJSON)){
//do sth with a single request here
//e.g. print(request.discipline)
}
}
}
jsonData
是一个 AnyObject
,您需要将其转换为 [[String: AnyObject]]
:
if let jsonData = response.result.value as? [[String: AnyObject]] {
for requestJSON in jsonData {
if let request = SingleRequest.fromJSON(requestJSON){
//do sth with a single request here
//e.g. print(request.discipline)
}
}
}
错误的意思是因为 response.result.value
默认是一个 AnyObject
,所以它是不可迭代的。这就是为什么您需要将其转换为数组(例如字典数组:[[String: AnyObject]]
)。
我有一个函数可以从 swift 中的 json 节点创建对象:
class func fromJSON(json: JSON) -> SingleRequest? {
var title: String
if let titleOrNil = json["title"].string {
title = titleOrNil
} else {
title = ""
}
let locationName = json["location"].string
let discipline = json["discipline"].string
let lat = json["location"]["coordinates"][1].doubleValue
let lon = json["location"]["coordinates"][0].doubleValue
let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
return SingleRequest(title: title, locationName: locationName!, discipline: discipline!, coordinate: coordinate)
}
现在,我正在尝试使用 alamofire
和 swiftyJson
从我的网络服务中获取所有数据并创建 SingleRequest
对象。我这样做如下所示:
func fetchRequests(radius: Double, lat: Double, lon: Double){
Alamofire.request(.GET, "https://mywebservice")
.responseJSON { response in
switch response.result {
case .Success:
if let jsonData = response.result.value {
for requestJSON in jsonData {
if let request = SingleRequest.fromJSON(requestJSON){
//do sth with a single request here
//e.g. print(request.discipline)
}
}
}
case .Failure(let error):
print("SWITCH ERROR")
print(error)
}
}
}
但我收到一个错误:
所以我的问题是 - 如何使用 alamoFire
和 SwiftyJson
创建自定义 SingleRequest
?
诚然,我不知道 "type" response.result.value 是什么,但如果它可以转换为可以迭代的东西,那么这里有一个例子:
if let jsonData = response.result.value as? [(some type that can be iterated)] {
// do something with jsonData ...
}
你的问题在这里:
if let jsonData = response.result.value {
for requestJSON in jsonData {
if let request = SingleRequest.fromJSON(JSON(requestJSON)){
//do sth with a single request here
//e.g. print(request.discipline)
}
}
}
jsonData
是一个 AnyObject
,您需要将其转换为 [[String: AnyObject]]
:
if let jsonData = response.result.value as? [[String: AnyObject]] {
for requestJSON in jsonData {
if let request = SingleRequest.fromJSON(requestJSON){
//do sth with a single request here
//e.g. print(request.discipline)
}
}
}
错误的意思是因为 response.result.value
默认是一个 AnyObject
,所以它是不可迭代的。这就是为什么您需要将其转换为数组(例如字典数组:[[String: AnyObject]]
)。