I am following this post for getting JSON data and for some reason this function:
func getJSON(urlToRequest: String) -> NSData {
return NSData(contentsOfURL: NSURL(string: urlToRequest)!)!
}
returns nil
as I get the error:
fatal error: unexpectedly found nil while unwrapping an Optional value
I have broken up the function:
func getJSON(urlToRequest: String) -> NSData {
let url : NSURL! = NSURL(string: urlToRequest)
let data : NSData! = NSData(contentsOfURL: url)
return data
}
And according to my debugger:
Printing description of url:
www.reddit.com/r/earthporn/.json
Printing description of data:
(NSData!) data = nil
Pasting in the URL to Chrome doesn't give a blank page. It gives a massive glob of JSON, so I am confused why data
is nil?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…