Home > Software design >  I can't pull Json data while doing MVVM design project with swift
I can't pull Json data while doing MVVM design project with swift

Time:03-25

I am making a project in Swift with MVVM design. I want to get coin name, current price, Rank and Symbol from a Crypto site. I can't show the json data I get on the console. The model is in another folder because I did it with MVVM. How can I create a struct to get the data here? You can find screenshots of my project below. I would be glad if you help.

Below are the codes I wrote in my web service file

import Foundation

class WebService {
    

    func downloadCurrencies(url: URL, completion: @escaping ([DataInfo]?) -> ()) {
        
        URLSession.shared.dataTask(with: url) { (data, response, error) in
            
            if let error = error {
                print(error.localizedDescription)
                completion(nil)
                
            } else if let data = data {
                
                let cryptoList = try? JSONDecoder().decode([DataInfo].self, from: data)
                
                print(cryptoList)
                
                if let cryptoList = cryptoList {
                    completion(cryptoList)
                }
            }
            
        }
        .resume()
    }
}

Below are the codes I wrote in my model file

import Foundation

struct DataInfo : Decodable {
    
    var name: String
    var symbol: String
    var cmc_rank: String
    var usd: Double
    
}

Finally, here is the code I wrote to print the data in the viewController to my console. But unfortunately I can't pull the data.

override func viewDidLoad() {
    super.viewDidLoad()
   
    let url = URL(string: "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=10&convert=USD&CMC_PRO_API_KEY=5ac24b80-27a1-4d01-81bd-f19620533480")!
    
    WebService().downloadCurrencies(url: url) { cryptos in
        if let cryptos = cryptos {
            print(cryptos)

        }
    }
}

CodePudding user response:

I've seen your URL and tested it ON Postman and also i've got your code and tried to put it in a shape, the code is good, but it can't MAP JSON Data against your struct because this is the json Data from Postman Postman Data

While Looking at your Struct, It Doesnt Match the format of JSON Data you're receiving,

Struct

Well, To Make your structure match the JSON String you need to create Nested String: Any Dictionary. But there's another issue with the logic, you need to decode data outside of the webservice call because it can contain errors which wont be mapped in the struct and can handle if you get other statusCode.

If you try to implement all these things manually, the code will become complex and hard to understand. I would rather recommend you to use Alamofire with SwiftyJSON and it can make your work a lot shorter and easier and understandable.

Sorry for the bad english.

CodePudding user response:

Your api-key is not valid for me. Your data must be inside of object or invalid keys and you are surely missing it thats why it is not parsing correctly.

My suggestion is to put your json response in this website

"https://app.quicktype.io/"

and replace your struct with new one, you will be good to go hopefully.

  • Related