The XMLParser subclass crashes with the contentsOfURL initializer if there is no connection, the Internet or not, to get the contents of the URL

I am trying to parse the issue of where the parsing framework ( FeedKit ) is if there is no connection to get the contents of the specified URL (for example, the application is disconnected).

So, it works when the application is connected to the network, and only on the Internet.

Whenever I try to create an instance of the Parser class using the convenience initializer of the XMLParser superclass

 convenience init?(contentsOf url: URL) 

Structure Failure:

enter image description here

To try to isolate the problem and eliminate some errors introduced into the structure, I recreated the problem in a clean project:

A solution that works like a charm using the Foundation Foundationโ€™s simple valid XMLParser:

 let feedURL = URL(string: "http://images.apple.com/main/rss/hotnews/hotnews.rss")! if let parser = XMLParser(contentsOf: feedURL) { // Works as expected print("Got instance \(parser)") } 

And another thing that does not:

 class Parser: XMLParser { } let feedURL = URL(string: "http://images.apple.com/main/rss/hotnews/hotnews.rss")! if let parser = Parser(contentsOf: feedURL) { // Crash! print("Got instance \(parser)") } 

In the second example, all I'm doing is a subclass of the XMLParser class. No overrides or custom code. And he still falls.

Did I miss something?

thanks

Edit:

I sent an Apple bug report with the number 28904764 and opened Radar for this problem.

I am sure that this is a mistake at the end of Apple, but would prefer to make mistakes and fix it.

+6
source share
2 answers

It looks like an error.

However, you can override the assigned and convenient XMLParser class XMLParser in Parser and implement your own logic

 class Parser: XMLParser { override init(data: Data) { super.init(data: data) } convenience init?(contentsOf url: URL){ //return data or null examining data from url do{ let data = try Data(contentsOf: url) self.init(data: data) }catch{ return nil } } } 

And is called as

  let feedURL = URL(string: "http://images.apple.com/main/rss/hotnews/hotnews.rss") if let parser = Parser(contentsOf: feedURL!){ print("Got instance \(parser)") }else{ print("no data in url") } 
+2
source

I think this was a problem with the URL. I tried ur code, i got error.

 Unable to read data 

I tried replacing the url,

 feedURL = NSURL(string:"http://images.apple.com/main/rss/hotnews/hotnews.rss#sthash.fuVEonEt.dpuf")! parser = XMLParser(contentsOf:feedURL as URL)! parser.delegate = self parser.parse() 

Everything works perfectly. (I am using Swift 3)

-one
source

Source: https://habr.com/ru/post/1011589/


All Articles