Swift 2错误,转换为JSON

时间:2015-09-30 15:54:01

标签: ios json swift2 swifty-json

我一直在使用swift 2时遇到错误,它很简单,但我无法得到它

这是我开始为Swift 2更新它之前的代码:

func updateWeatherInfo(latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
    let url = "http://api.openweathermap.org/data/2.5/forecast"
    let params = ["lat":latitude, "lon":longitude]
    println(params)

Alamofire.request(.GET, url, parameters: params)
    .responseJSON { (request, response, json, error) in
        if(error != nil) {
            println("Error: \(error)")
            println(request)
            println(response)
            self.loading.text = "Internet appears down!"
        }
        else {
            println("Success: \(url)")
            println(request)
            var json = JSON(json!)
            self.updateUISuccess(json)
        }
    }
}

这是投掷线错误:

let json = JSON(json)

这是我开始后的整个功能

    func updateWeatherInfo(latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
        let url = "http://api.openweathermap.org/data/2.5/forecast"
        let params = ["lat":latitude, "lon":longitude]
        print(params)

        Alamofire.request(.GET, url, parameters: params)
            .responseJSON { (request, response, json) in

                    print("Success: \(url)")
                    print(request)

                    let json = JSON(json)
                    self.updateUISuccess(json!)

        }
    }

updateUISuccess需要一个JSON值,但目前我有一个(请求)

继承人的错误说明 enter image description here

在提出建议后我尝试了这个:

.Success(let data):
                    let data_ar = data as! JSON // or NSDictionary or NSString
                    self.updateUISuccess(data_ar)

                case .Failure(let data, let error):
                    print("Request failed with error: \(error)")


                }

但应用程序在let data_ar = data as! JSON // or NSDictionary or NSString

崩溃

1 个答案:

答案 0 :(得分:0)

变量json看起来像枚举值。试试这个:

switch json {
    case .Success(let data):
        let data_ar = data as! NSArray // or NSDictionary or NSString

    case .Failure(let data, let error):
        print("Request failed with error: \(error)")


    }