简体   繁体   English

为什么我不能使用AlamoFire和SwiftyJson快速创建班级对象?

[英]Why I can't create objects of my class in swift using AlamoFire and SwiftyJson?

I have a function that creates an object from json node in 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)
}

now, using alamofire and swiftyJson I'm trying to fetch all data from my webservice and create SingleRequest objects. 现在,我正在尝试使用alamofireswiftyJson从Web服务中获取所有数据并创建SingleRequest对象。 I do it as shown below: 我这样做如下所示:

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)
            }

    }
}

but I'm getting an error: 但出现错误:

在此处输入图片说明

So my question is - how, using alamoFire and SwiftyJson I can create my custom SingleRequest ? 所以我的问题是-如何使用alamoFireSwiftyJson创建自定义的SingleRequest

Your problem is here: 您的问题在这里:

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 is an AnyObject that you need to cast to [[String: AnyObject]] : jsonDataAnyObject ,你需要转换为[[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)
        }
    }
}

What the error is saying is that since response.result.value is an AnyObject by default, it is not iterable. 错误的意思是,由于默认情况下response.result.valueAnyObject ,因此它是不可迭代的。 That's why you need to cast it to an array (eg an array of dictionaries: [[String: AnyObject]] ). 这就是为什么您需要将其[[String: AnyObject]]转换为数组(例如,字典数组: [[String: AnyObject]] )。

Admittedly, I don't know what "type" response.result.value is, but if it can be cast to something that can be iterated then here's an example of that: 诚然,我不知道什么是“类型” response.result.value,但是如果可以将其强制转换为可以迭代的对象,那么下面是一个示例:

if let jsonData = response.result.value as? [(some type that can be iterated)] {
    // do something with jsonData ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM