我使用json解析获得以下数据,但无法在放置在情节提要中的导航栏标题,pricelabel和textview上显示。如何将图像组中的图像也放入其中。有谁能够帮助我 ?

class ViewController: UIViewController {

    @IBOutlet weak var navigationbar: UINavigationBar!
    @IBOutlet weak var productImage: UIImageView!
    @IBOutlet weak var productPricelbl: UILabel!
    @IBOutlet weak var productDescriptionlbl: UITextView!

    var productName :String?
    var productprice :String?
    var productdescription :String?
    var thumbnailimageArray = [String]()
    var imageArray = [String]()
    let urlString = "http://www.json-generator.com/api/json/get/cjpberBhKa?indent=2"

    override func viewDidLoad() {
        super.viewDidLoad()
        self.downloadJsonWithURL()
//        let imgURL = NSURL(string:imageArray[0])
//        if imgURL != nil {
//            let data = NSData(contentsOf: (imgURL as URL?)!)
//            self.productImage.image = UIImage(data: data! as Data)
//        }
        self.navigationbar.topItem?.title = productName
        productDescriptionlbl.text = productdescription
        productPricelbl.text = productprice

        // Do any additional setup after loading the view, typically from a nib.
    }
    @IBAction func arrowmarkButton(_ sender: Any) {

    }
    func downloadJsonWithURL() {
        let url = NSURL(string: urlString)
        URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
            if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
                print(jsonObj!.value(forKey: "Detail")!)
                if let detailsArray = jsonObj!.value(forKey: "Detail") as? NSArray {
                        if let detailDict = detailsArray[0] as? NSDictionary {
                            if let name = detailDict.value(forKey: "productName") {
                                self.productName = name as? String
                            }
                            if let thumbnailimage1 = detailDict.value(forKey: "thumnail1"){
                                self.thumbnailimageArray.append(thumbnailimage1 as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail2"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail3"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail4"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail5"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail6"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail7"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail8"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail9"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail10"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let image1 = detailDict.value(forKey: "image1"){
                                self.imageArray.append(image1 as! String)
                            }
                            if let image2 = detailDict.value(forKey: "image2"){
                                self.imageArray.append(image2 as! String)
                            }
                            if let image3 = detailDict.value(forKey: "image3"){
                                self.imageArray.append(image3 as! String)
                            }
                            if let image4 = detailDict.value(forKey: "image4"){
                                self.imageArray.append(image4 as! String)
                            }
                            if let image5 = detailDict.value(forKey: "image5"){
                                self.imageArray.append(image5 as! String)
                            }
                            if let image6 = detailDict.value(forKey: "image6"){
                                self.imageArray.append(image6 as! String)
                            }
                            if let image7 = detailDict.value(forKey: "image7"){
                                self.imageArray.append(image7 as! String)
                            }
                            if let image8 = detailDict.value(forKey: "image8"){
                                self.imageArray.append(image8 as! String)
                            }
                            if let image9 = detailDict.value(forKey: "image9"){
                                self.imageArray.append(image9 as! String)
                            }
                            if let image10 = detailDict.value(forKey: "image10"){
                                self.imageArray.append(image10 as! String)
                            }
                            if let price = detailDict.value(forKey: "productPrice") {
                                self.productprice = price as? String
                            }
                            if let description = detailDict.value(forKey: "productDes") {
                                self.productdescription = description as? String
                                print(self.productdescription)
                            }
                        }
                }
                OperationQueue.main.addOperation({
                })
            }
        }).resume()
    }
}

最佳答案

您所做的错误是您正在更新后台线程中的UI元素。如果要更新UI元素,请首先进入主线程,然后更新UI元素。

现在尝试这种方式

class ViewController: UIViewController {

    @IBOutlet weak var navigationbar: UINavigationBar!
    @IBOutlet weak var productImage: UIImageView!
    @IBOutlet weak var productPricelbl: UILabel!
    @IBOutlet weak var productDescriptionlbl: UITextView!

    var productName :String?
    var productprice :String?
    var productdescription :String?
    var thumbnailimageArray = [String]()
    var imageArray = [String]()
    let urlString = "http://www.json-generator.com/api/json/get/cjpberBhKa?indent=2"

    override func viewDidLoad() {
        super.viewDidLoad()
        self.downloadJsonWithURL()
//        let imgURL = NSURL(string:imageArray[0])
//        if imgURL != nil {
//            let data = NSData(contentsOf: (imgURL as URL?)!)
//            self.productImage.image = UIImage(data: data! as Data)
//        }


        // Do any additional setup after loading the view, typically from a nib.
    }
    @IBAction func arrowmarkButton(_ sender: Any) {

    }
    func downloadJsonWithURL() {
        let url = NSURL(string: urlString)
        URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
            if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
                print(jsonObj!.value(forKey: "Detail")!)
                if let detailsArray = jsonObj!.value(forKey: "Detail") as? NSArray {
                        if let detailDict = detailsArray[0] as? NSDictionary {
                            if let name = detailDict.value(forKey: "productName") {
                                self.productName = name as? String
                            }
                            if let thumbnailimage1 = detailDict.value(forKey: "thumnail1"){
                                self.thumbnailimageArray.append(thumbnailimage1 as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail2"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail3"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail4"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail5"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail6"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail7"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail8"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail9"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let thumbnailimage = detailDict.value(forKey: "thumnail10"){
                                self.thumbnailimageArray.append(thumbnailimage as! String)
                            }
                            if let image1 = detailDict.value(forKey: "image1"){
                                self.imageArray.append(image1 as! String)
                            }
                            if let image2 = detailDict.value(forKey: "image2"){
                                self.imageArray.append(image2 as! String)
                            }
                            if let image3 = detailDict.value(forKey: "image3"){
                                self.imageArray.append(image3 as! String)
                            }
                            if let image4 = detailDict.value(forKey: "image4"){
                                self.imageArray.append(image4 as! String)
                            }
                            if let image5 = detailDict.value(forKey: "image5"){
                                self.imageArray.append(image5 as! String)
                            }
                            if let image6 = detailDict.value(forKey: "image6"){
                                self.imageArray.append(image6 as! String)
                            }
                            if let image7 = detailDict.value(forKey: "image7"){
                                self.imageArray.append(image7 as! String)
                            }
                            if let image8 = detailDict.value(forKey: "image8"){
                                self.imageArray.append(image8 as! String)
                            }
                            if let image9 = detailDict.value(forKey: "image9"){
                                self.imageArray.append(image9 as! String)
                            }
                            if let image10 = detailDict.value(forKey: "image10"){
                                self.imageArray.append(image10 as! String)
                            }
                            if let price = detailDict.value(forKey: "productPrice") {
                                self.productprice = price as? String
                            }
                            if let description = detailDict.value(forKey: "productDes") {
                                self.productdescription = description as? String
                                print(self.productdescription)
                            }
                        }
                }
                OperationQueue.main.addOperation({
                 self.navigationbar.topItem?.title = self.productName
                 self.productDescriptionlbl.text = self.productdescription
                 self.productPricelbl.text = self.productprice
                })
            }
        }).resume()
    }
}

关于ios - 在这里我无法显示从json解析获得的数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43999029/

10-09 02:10