获取新 Alamofire Swift 3 的请求
Posted
技术标签:
【中文标题】获取新 Alamofire Swift 3 的请求【英文标题】:GET request on new Alamofire Swift 3 【发布时间】:2016-10-16 15:02:16 【问题描述】:所以我决定将我的应用程序迁移到 Swift 3.0,但现在我发现自己无法解决使用新 Alamofire 的 .GET 请求所面临的问题。这是现在的请求:
func getAlbums(_ ID: String, ANN: String, completed: @escaping DownloadComplete)
let searchFT = ID.replacingOccurrences(of: " ", with: "+", options: NSString.CompareOptions.literal, range: nil)
let urlPath = ""
let url = URL(string: urlPath)
Alamofire.request(url!, method: .get).responseJSON response in
if let JSON = response.result.value as! [String: AnyObject]!
if let dict = JSON["results"] as! NSArray!
AlbumsArtist.removeAll()
AlbumsArtist = [String]()
ImagesArtist.removeAll()
ImagesArtist = [String]()
for items in dict
if let AT = items["artistName"] as? String
if AT == ANN
if let TN = items["collectionName"] as? String
AlbumsArtist.append(TN)
else
AlbumsArtist.append("")
if let IM = items["artworkUrl100"] as? String
let newString = IM.stringByReplacingOccurrencesOfString("100", withString: "600", options: NSStringCompareOptions.LiteralSearch, range: nil)
ImagesArtist.append(newString)
else
ImagesArtist.append("")
completed()
我遇到的问题是如果让AT = items["artistName"] as? String
错误说:Type 'NSFastEnumerationIterator.Element (aka: 'Any') has no subscript members
非常感谢!
【问题讨论】:
看这个答案***.com/a/39429455/5327882 @ronatory 已修复!谢谢! 【参考方案1】:如评论中所述,您应该将代码更改为:
if let AT = items["artistName"] as? [[String:Any]]
但为了让您的代码更安全,您应该使用以下内容:
if let AT = items["artistName"] as? [[String:Any]]
if AT == ANN
guard let TN = items["collectionName"] as? String,
let IM = items["artworkUrl100"] as? String else // Some error occurred
AlbumsArtist.append(TN);
let newString = IM.stringByReplacingOccurrencesOfString("100", withString: "600", options: NSStringCompareOptions.LiteralSearch, range: nil)
ImagesArtist.append(newString)
【讨论】:
以上是关于获取新 Alamofire Swift 3 的请求的主要内容,如果未能解决你的问题,请参考以下文章
swift 3 alamofire - 获取请求给出响应序列化失败
带参数的 Alamofire 4 Swift 3 GET 请求