使用 Alamofire 搜索从 swift 2 转换为 swift 3 时出错
Posted
技术标签:
【中文标题】使用 Alamofire 搜索从 swift 2 转换为 swift 3 时出错【英文标题】:Error converting from swift 2 to swift 3 with searching using Alamofire 【发布时间】:2016-11-26 21:39:10 【问题描述】: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.
非常感谢任何帮助。
【问题讨论】:
您能解决问题吗? 不,我没有,它没用 检查我更新的答案。 【参考方案1】:喜欢下面的?
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】:试试:
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)")
【讨论】:
【参考方案3】:对于 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)")
【讨论】:
以上是关于使用 Alamofire 搜索从 swift 2 转换为 swift 3 时出错的主要内容,如果未能解决你的问题,请参考以下文章
Alamofire 自定义响应从 Alamofire v1.3 迁移到 3.0(和 Swift 2 语法)
Alamofire - 从 Swift 2 转换为 Swift 3 时出错
在 swift 中使用 Alamofire 取消 api 请求
如何将 json 数据从 alamofire 转换为 swift 对象