-1

I am getting data from news API. Everything was working like 40+ times then something happened and nothing was working. After I turn on mac on next day it worked a few times and now again doesn’t, I don't know why is this. please help, code is below :

I tried to reinstall Xcode, manage some plist, rewrite the project.

struct Model: Codable {
    let articles: [News]
}

struct Source: Codable {
    var name: String?
    var id: String?
}

struct News: Codable {
    var title: String?
    var url: String
    var description: String
    var urlToImage: URL?
    var source: Source
    var publishedAt: String?
    var author: String?
    var content: String?
}


class myData {
    static func getData(completion: @escaping ([News]) -> ()) {
        DispatchQueue.global(qos: .userInteractive).async {
            var finalData = [News]()

            let jsonUrlString = "https://newsapi.org/v2/top-headlines?country=ua&apiKey=0cff1368c1d1445d9a0bccb6063a5220"
            if let url = URL(string: jsonUrlString) {
                if let data = try? Data(contentsOf: url) {

                    let decoder = JSONDecoder()
                    if let jsonPetitions = try? decoder.decode(Model.self, from: data) {
                        finalData = jsonPetitions.articles
                    }
                }
            }
            DispatchQueue.main.async {
                completion(finalData)
            }
        }
    }
}

my code is reloading tableView and every time it comes empty. it should look like simple json.

Nirav Kotecha
  • 2,493
  • 1
  • 12
  • 26
  • Never use `Data(contentsOf:)` for remote resources. Download the data using `URLSession`, then you'll be able to see and handle any errors that might occur. – Gereon May 07 '19 at 09:29
  • it doesn't helped i tried code with urlSessions but it doesn't even go to cathc so i jsut recieve an empty array – Mark Pryssiaznuk May 07 '19 at 09:47
  • 1
    Don't just look at caught errors, also inspect the error parameter from the URLSessionTask closure. – Gereon May 07 '19 at 10:07
  • Define stopped working: `finalData` empty? There are 2 try not caught ? Any logs? – Larme May 07 '19 at 10:19
  • Try deducing the error in catch block using `try Data(contentsOf: url)` instead of `try? Data(contentsOf: url)`. – justintime May 07 '19 at 11:39

1 Answers1

0

You have a decoding error. It'll be easily deducible if you catch the decoding error. The description property is null for one of the news source in your response. Just make it Optional.

Try this:

struct Model: Codable {
    let articles: [News]
}

struct Source: Codable {
    var name: String?
    var id: String?
}

struct News: Codable {
    var title: String?
    var url: String
    var description: String?
    var urlToImage: String?
    var source: Source
    var publishedAt: String?
    var author: String?
    var content: String?
}


class MyData {
    static func getData(completion: @escaping ([News]) -> ()) {
        DispatchQueue.global(qos: .userInteractive).async {
            var finalData = [News]()

            let jsonUrlString = "https://newsapi.org/v2/top-headlines?country=ua&apiKey=0cff1368c1d1445d9a0bccb6063a5220"
            if let url = URL(string: jsonUrlString) {

                do {
                    let data = try? Data(contentsOf: url)

                    let decoder = JSONDecoder()
                    let jsonPetitions = try decoder.decode(Model.self, from: data!)
                    finalData = jsonPetitions.articles
                } catch {
                    print(error)
                }


            }
            DispatchQueue.main.async {
                completion(finalData)
            }
        }
    }
}

MyData.getData { (news) in
    print(news)
}
justintime
  • 352
  • 3
  • 11