使用Alamofire进行搜索时,从swift 2转换为swift 3时出错

时间:2016-11-26 21:39:11

标签: ios swift xcode alamofire

func getLastVideos(){

   Alamofire.request(url: urlString, method: .GET, parameters: ["part":"snippet","maxResults":"30","key":API_KEY, "playlistId": playlist_Id], encoding: URLEncoding.default, headers: nil).responseJSON { (response) in



        if let jsonResult = response.result.value{

            var videosArray = [LastVideos]()

            for video in jsonResult["items"] as! NSArray{

                let lastVideo = LastVideos()

                lastVideo.title = video.valueForKeyPath("snippet.title") as! String
                lastVideo._description = video.valueForKeyPath("snippet.description") as! String
                lastVideo.id = video.valueForKeyPath("snippet.resourceId.videoId") as! String

                if video.valueForKeyPath("snippet.thumbnails.maxres.url") != nil {
                    lastVideo.thumbnailUrl = video.valueForKeyPath("snippet.thumbnails.maxres.url") as! String
                }else if video.valueForKeyPath("snippet.thumbnails.standard.url") != nil {
                    lastVideo.thumbnailUrl = video.valueForKeyPath("snippet.thumbnails.standard.url") as! String
                }else if video.valueForKeyPath("snippet.thumbnails.high.url") != nil {
                    lastVideo.thumbnailUrl = video.valueForKeyPath("snippet.thumbnails.high.url") as! String
                }else if video.valueForKeyPath("snippet.thumbnails.medium.url") != nil {
                    lastVideo.thumbnailUrl = video.valueForKeyPath("snippet.thumbnails.medium.url") as! String
                }else{
                    lastVideo.thumbnailUrl = video.valueForKeyPath("snippet.thumbnails.default.url") as! String
                }

                videosArray.append(lastVideo)

            }

            self.lastVideos = videosArray


            if self.delegate != nil {
                self.delegate.lastVideosAreReady()

            }





        }
    }

我似乎无法将此代码与swift 3和alamofire 4.0配合使用。 我一直收到extra argument in call的错误。一旦我修复了该错误,我就会收到错误:type any has no subscript members in the if let statement.

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

尝试:

if let jsonResult = response.result.value as? [String:AnyObject]

在Alamofire Closure中使用以下代码行:

switch response.result {
case .Success(let JSON):
    print("Success with JSON: \(JSON)")
    //rest of code

case .Failure(let error):
    print("Error with Description: \(error.localizedDescription)")
}

答案 1 :(得分:0)

喜欢以下内容?

let url = "www.mywebsite.net/api/"
let params = ["part":"snippet", "maxResults":"30", "key":API_KEY, "playlistId": playlist_Id]
Alamofire.request(.GET, url, parameters: params).responseJSON { (responseObject) -> Void in
    guard let object = response.result.value else {
        return
    }
    let json = JSON(object) // with SwiftyJSON
}

答案 2 :(得分:0)

对于swift 3,请使用以下代码。它在我的所有项目中都运作良好..

//参数

let paramDic : NSMutableDictionary = [
            "key1" : "value1",
            "key2" : "value2",
        ]

let urlString = "www.mywebsite.net/api/"
let url : URL = URL.init(string: urlString)!
var request = URLRequest(url: url)
request.httpMethod = "GET"

//Setting header

request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")

//Setting parameters

request.httpBody = try! JSONSerialization.data(withJSONObject: paramDic!, options: [])



if paramDic != nil {
            request.httpBody = try! JSONSerialization.data(withJSONObject: paramDic!, options: [])
        }

alamoFireManager!.request(request as URLRequestConvertible).responseJSON {
            response in
            switch response.result {
            case .failure(let error):
                print("\(urlString) failure response -> \n NSHTTPURLResponse ->\(response.response) \n \(error)")
            case .success :
                print("\(urlString) success response -> \n NSHTTPURLResponse ->\(response.response) \n Data -> \(response.result.value as? NSDictionary)")
            }
        }