ios - 新 Alamofire Swift 3 上的 GET 请求

标签 ios swift swift3 alamofire

因此,我决定将我的应用程序迁移到 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()

                }

            }

            }
        }

我遇到的问题是 if let AT = items["artistName"] as? String 错误是这样的: Type 'NSFastEnumerationIterator.Element (aka: 'Any') has no subscript Members

非常感谢!

最佳答案

正如评论中提到的,您应该将代码更改为:

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

关于ios - 新 Alamofire Swift 3 上的 GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40071741/

相关文章:

ios - 如何覆盖 swift3 中的高分

ios - 使用 Storyboard在 iOS 6 中使用 uiscrollview 分页时遇到问题

ios - event.allTouches()?.allObjects.last

objective-c - ASIHTTPRequest 和 NSURLRequest 不同的结果

ios - DropboxSDK : unable to link error

ios - UITableViewCell 内多个堆栈 View 内的多行标签

iphone - Objective c - 使用一个或多个 ViewController 在 View 之间翻转?

swift - Xcode Release/Swift 条件代码

arrays - 与 Swift 中的另一个数组相比对数组重新排序

ios - Swift3 框架模块为空