Group Group Group Group Group Group Group Group Group

Errors and Issues with raywenderlich's Final Version of Headlines app

#1

I was testing with the Headlines app that I had completed from the starter version, but the completed app still crash and other issues with some of the news sources.
So I went and tested the final version of Headlines app that came with the downloaded source code, and the same problem occurred with raywenderlich’s final version as well. Here are the news sources that either crash the app or did not crash but with issues indicated in the console output.
Most are valueNotFound errors, one with dataCorrupted.

The decoding errors are associated with this statement:
if let articles = try! decoder.decode(Response.self, from: data).articles {
self.articles = articles
}

Is there any way to correct the app to make it run correctly?

Al Jazeera English:
Thread 4: Fatal error: ‘try!’ expression unexpectedly raised an error: Swift.DecodingError.valueNotFound(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: “articles”, intValue: nil), _JSONKey(stringValue: “Index 4”, intValue: 4), CodingKeys(stringValue: “description”, intValue: nil)], debugDescription: “Expected String value but found null instead.”, underlyingError: nil))

Bloomberg:
Thread 8: Fatal error: ‘try!’ expression unexpectedly raised an error: Swift.DecodingError.valueNotFound(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: “articles”, intValue: nil), _JSONKey(stringValue: “Index 1”, intValue: 1), CodingKeys(stringValue: “description”, intValue: nil)], debugDescription: “Expected String value but found null instead.”, underlyingError: nil))

Financial Times:
Thread 2: Fatal error: ‘try!’ expression unexpectedly raised an error: Swift.DecodingError.valueNotFound(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: “articles”, intValue: nil), _JSONKey(stringValue: “Index 4”, intValue: 4), CodingKeys(stringValue: “description”, intValue: nil)], debugDescription: “Expected String value but found null instead.”, underlyingError: nil))

Football Italia:
Thread 4: Fatal error: ‘try!’ expression unexpectedly raised an error: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: “articles”, intValue: nil), _JSONKey(stringValue: “Index 2”, intValue: 2), CodingKeys(stringValue: “urlToImage”, intValue: nil)], debugDescription: “Invalid URL string.”, underlyingError: nil))

Fortune: Did not crash, but with this console output - 2018-07-07 12:06:52.375811-0700 Headlines[24691:1512052] Task .<8> finished with error - code: -1002

Fox Sports:
Thread 20: Fatal error: ‘try!’ expression unexpectedly raised an error: Swift.DecodingError.valueNotFound(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: “articles”, intValue: nil), _JSONKey(stringValue: “Index 2”, intValue: 2), CodingKeys(stringValue: “description”, intValue: nil)], debugDescription: “Expected String value but found null instead.”, underlyingError: nil))

Hacket News:
Thread 3: Fatal error: ‘try!’ expression unexpectedly raised an error: Swift.DecodingError.valueNotFound(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: “articles”, intValue: nil), _JSONKey(stringValue: “Index 0”, intValue: 0), CodingKeys(stringValue: “description”, intValue: nil)], debugDescription: “Expected String value but found null instead.”, underlyingError: nil))

Independent:
Thread 10: Fatal error: ‘try!’ expression unexpectedly raised an error: Swift.DecodingError.valueNotFound(Foundation.URL, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: “articles”, intValue: nil), _JSONKey(stringValue: “Index 7”, intValue: 7), CodingKeys(stringValue: “urlToImage”, intValue: nil)], debugDescription: “Expected URL value but found null instead.”, underlyingError: nil))

National Geographic: Did not crash, but with console outputs -
2018-07-07 12:13:47.079312-0700 Headlines[24788:1518027] init:1303: *** ImageIO AppleJPEG decode failed - falling back to libJEG

Newsweek:
Thread 13: Fatal error: ‘try!’ expression unexpectedly raised an error: Swift.DecodingError.valueNotFound(Foundation.URL, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: “articles”, intValue: nil), _JSONKey(stringValue: “Index 5”, intValue: 5), CodingKeys(stringValue: “urlToImage”, intValue: nil)], debugDescription: “Expected URL value but found null instead.”, underlyingError: nil))

The Telegraph: Did not crash, but with console outputs -
2018-07-07 12:17:36.204447-0700 Headlines[24817:1524051] TIC Read Status [39:0x604000178000]: 1:57

Time: Did not crash, but with console outputs -
2018-07-07 12:19:54.862639-0700 Headlines[24817:1525822] TIC Read Status [62:0x0]: 1:57

USA Today: Did not crash, but with console output -
2018-07-07 12:25:37.190947-0700 Headlines[24817:1529885] Task <0EE0976F-8A67-412C-B947-9122BD09C740>.<294> finished with error - code: -999

Wirtschafts Woche:
Thread 79: Fatal error: ‘try!’ expression unexpectedly raised an error: Swift.DecodingError.valueNotFound(Foundation.URL, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: “articles”, intValue: nil), _JSONKey(stringValue: “Index 1”, intValue: 1), CodingKeys(stringValue: “urlToImage”, intValue: nil)], debugDescription: “Expected URL value but found null instead.”, underlyingError: nil))

#2

Update: So after I input the news sources url into the web browser, I realized the reason that the above news sources that cause crash to the app because of the valueNotFound error is because they have null urlToImage keys for some of their news articles. So I thought maybe I could replace the null urlToImage with a url of an image not available image, or remove the news articles with null urlToImage keys, but I still can’t figure out how to do that, and I’ve been working on it for the past 5 hours.

#3

Some of the sites are returning null for some values, including urlToImage. It can show up just about anywhere. Best bet seems to be making all the properties optional, and using decodeIfPresent() on all of them:

class Article: NSObject, Codable {
  let author: String?
  let title: String?
  let snippet: String?
  let sourceURL: URL?
  let imageURL: URL?
  let published: Date?

and

  required init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    author = try container.decodeIfPresent(String.self, forKey: .author)
    title = try container.decodeIfPresent(String.self, forKey: .title)
    let rawSnippet = try container.decodeIfPresent(String.self, forKey: .snippet)
    snippet = rawSnippet?.deletingCharacters(in: CharacterSet.newlines)
    sourceURL = try container.decodeIfPresent(URL.self, forKey: .sourceURL)
    imageURL = try container.decodeIfPresent(URL.self, forKey: .imageURL)
    published = try container.decodeIfPresent(Date.self, forKey: .published)
  }
1 Like
#4

Hi, gerrard
Thank you for your feedback. I’ve tried your suggestion, and the app is working much better. I’m going to try to modify the app to accommodate the new version 2 of the API from newsapi.org.

#5

I attempted this as well.

NewsAPI.swift

private static let basePath = "https://newsapi.org/v2"

private func path() -> URL {
  switch self {
  case .sources:
    // Returns all named sources available
    return URL(string: "\(API.basePath)/sources?&apiKey=\(API.key)")!
  case .articles(let source):
    return URL(string: "\(API.basePath)/top-headlines?sources=\(source.id)&apiKey=\(API.key)")!
  }
}

in Source.swift
I added these properties so I could return a bunch of different filtered results for practice

url
language
country

And then changed the return URL in path() case .sources to one of the following

/// Returns Only USA - Country property needed
return URL(string: "\(API.basePath)/sources?country=us&apiKey=\(API.key)")!

/// Returns All ENGLISH Language & USA - Language & Country property needed
return URL(string: "\(API.basePath)/sources?language=encountry=us&apiKey=\(API.key)")!
        
/// Returns Only BBC
return URL(string: "\(API.basePath)/sources?id=bbc-news&apiKey=\(API.key)")!

I was able to populate the majority of new sources without hickups, except for the Associated Press I believe, I think the issue is that their first item returned is a list of authors, and not actually an article summary.

Cheers!
Rebecca

1 Like
#6

I am reading this Chapter and somehow Bloomberg articles’ json’s imageURL is null. That’s why the app crashes. Make sure you handle this in ArticleCell.swift

closed #7

This topic was automatically closed after 166 days. New replies are no longer allowed.