ios - swift 错误 : ambiguous reference to member 'jsonObject(with:options:)

标签 ios swift nsurlsession nsjsonserialization json-serialization

我正在尝试借助 GET 请求将图像从服务器加载到我的 iOS 应用程序中。但是,它报告错误:

ambiguous reference to member 'jsonObject(with:options:)

谁能帮忙解决这个错误?

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet var MyCollectionView: UICollectionView!

    var images : [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        loadImages()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //MyCollectionViewCell

        let myCell:MyCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath) as! MyCollectionViewCell



        DispatchQueue.global(qos: .background).async{

            let imageString = self.images[indexPath.row]
            let imageUrl = NSURL(string: imageString)
            let imageData = NSData(contentsOf: imageUrl! as URL)

            DispatchQueue.main.async {
                //if(imageData! = nil)
                //{

                    myCell.myImageView.image = UIImage(data: imageData! as Data)

                //}//end of if
            }//end of DispatchQueue.main.async




        };//end of main dispatch

        return myCell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("User tapped on image # \(indexPath.row)")
    }

    func loadImages()
    {
            let startTime = NSDate.timeIntervalSinceReferenceDate

            var pageUrl = "Url is here"
        let myUrl = NSURL(string: pageUrl)
        let request = NSMutableURLRequest(url:myUrl! as URL)

        let task = URLSession.shared.dataTask(with: request as URLRequest){
                data, response, error in

            //If error displays any alert message
            if error != nil {

                var myAlert = UIAlertController(title: "Alert", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)

                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)

                myAlert.addAction(okAction)

                self.present(myAlert, animated: true, completion: nil)

                return


            }

            var err: NSError?

            if let usabledata = data{
            do{
            var jsonArray = try JSONSerialization.jsonObject(with: usabledata, options: .mutableContainers) as! [String:AnyObject]
                print(jsonArray)

            //conditional binding error here
            if let parseJSONArray = jsonArray {

                self.images = parseJSONArray as! [String]

                DispatchQueue.main.async(execute: {
                    self.MyCollectionView.reloadData()

                })//end of dispatchQueue


            }//end of if
            }catch{
                print(error)

            }


        }
        }

        task.resume()

    }

}

最佳答案

试试这个:

替换为您的代码

if let usableData = data {
    do {
        var jsonArray = try JSONSerialization.jsonObject(with: usableData, options: .mutableContainers)  as! [String:AnyObject] 
        if let parseJSONArray = jsonArray as? [String] {
            // parse array here
        }    
    } catch {
        print("JSON Processing Failed")
    }
}

关于ios - swift 错误 : ambiguous reference to member 'jsonObject(with:options:),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45235427/

相关文章:

ios - 使部分标识符无效

ios - 滚动表格 View 时文本正在替换

ios - 在应用商店提交应用时本地通知不起作用

ios - 推送通知应用程序背景 iOS 9 swift

ios - 如何在 UITableViewCell 中设置 IndentationLevel?

swift - 多个 NSURLSession 依赖的下载任务

php - NSURLSessionUploadTask 没有将文件传递给 php 脚本

ios - NSURLSession错误中返回值

ios - 将数据从 xib 单元格传递到 View Controller

ios - 为什么我的 UITableView 不再工作了?