【问题标题】:why i am unable to download all json values in swift?为什么我无法快速下载所有 json 值?
【发布时间】:2019-10-16 13:40:07
【问题描述】:

我有后端 api 它包含所有值,我可以在邮递员中看到这些值.. 但是在解析时我无法从 api 下载所有值.. 有时我得到所有值.. 有时我不只得到一些值.. 如果我关闭应用程序并再次运行,那么我将获得所有值.. 如果我关闭并运行,或者如果我去其他视图控制器并回到家,那么我会丢失一些值。如果我打印 jsonObj 我没有从 api 获取所有值。为什么会发生这种情况?

这是我的代码:

import UIKit
import SDWebImage
struct JsonData {
var iconHome: String?
var typeName: String?
var id: String?
init(icon: String, tpe: String, id: String) {
    self.iconHome = icon
    self.typeName = tpe
    self.id = id
}
}
class HomeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITextFieldDelegate {

@IBOutlet weak var collectionView: UICollectionView!
var itemsArray = [JsonData]()
override func viewDidLoad() {
    super.viewDidLoad()
    homeServiceCall()
    //Do any additional setup after loading the view.
    collectionView.delegate = self
    collectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return itemsArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! HomeCollectionViewCell
    let aData = itemsArray[indexPath.row]
    cell.paymentLabel.text = aData.typeName
    cell.paymentImage.sd_setImage(with: URL(string:aData.iconHome ?? ""), placeholderImage: UIImage(named: "varun finance5_icon"))
    return cell
}
//MARK:- Service-call
func homeServiceCall(){

    let urlStr = "https://dev.com/webservices//getfinancer"
    let url = URL(string: urlStr)
    URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
        guard let respData = data else {
            return
        }
        guard error == nil else {
            print("error")
            return
        }
        do{
            let jsonObj = try JSONSerialization.jsonObject(with: respData, options: .allowFragments) as! [String: Any]
            print("the home json is \(jsonObj)")
            let financerArray = jsonObj["financer"] as! [[String: Any]]
            for financer in financerArray {
                guard let id = financer["id"] as? String else { break }
                guard let pic = financer["icon"] as? String else { break }
                guard let typeName = financer["tpe"] as? String else { break } //changed this one to optional too. Avoid force-unwrapping. Keep everything safe
                let jsonDataObj = JsonData(icon: pic, tpe: typeName, id: id)
                self.itemsArray.append(jsonDataObj)
            }
            DispatchQueue.main.async {
                self.collectionView.reloadData()
            }
        }
        catch {
            print("catch error")
        }
    }).resume()
}
}

请在上面的代码中帮助我。

【问题讨论】:

    标签: ios json swift rest uicollectionview


    【解决方案1】:

    尝试使用后台线程进行网络调用。

    DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
        self.homeServiceCall()            
    }
    

    URL 是合法的,双/ 如下?

    let urlStr = "https://dev.com/webservices//getfinancer"
    

    并检查所有后端数据是否与 JsonData 结构的 JSONSerialization 类型兼容。

    希望对你有帮助。

    【讨论】:

    • 在我的 json 中,我有一个 nil 图像,我没有得到剩余的图像。我现在检查了为什么?
    • 我没有在上面的帖子中发布原始 url。实际上我正在获取从 url 到 json 的所有值
    • 好的,你能分享一个你的json例子吗?哪些部分现在正在工作?
    • 没有。我在 json 中获取所有值.. 但我只能在 collectionview 中显示前十五个值
    • 我在 16 个中没有图像,我没有在 collectionview 中显示任何值
    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多