Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift handling NSData initialisation fails

I have the following line:

let jsonResults = NSData(contentsOfURL: Fetcher.URLforLicenseInfo())

This compiles and executes fine but if the initialisation of NSData fails I get an exception later in the code. I have tried to add another line:

if jsonResults != nil { ///blah, blah...

but then the compiler complains "Cannot invoke '!=' with an argument list type '(NSData, NilLiteralConvertible)"

Shouldn't the compiler recognise NSData initialiser return type as an optional?

like image 493
user2041042 Avatar asked Sep 22 '14 07:09

user2041042


1 Answers

check the length of the data : if(data.length > 0) ...

like image 146
Daij-Djan Avatar answered Sep 29 '22 05:09

Daij-Djan