NSData Initializer will never return nil . On the other hand, when you write jsonResults != nil , it means that jsonResults is expected Optional, but it is not.
The condition if jsonResults != nil {} does not make sense.
Instead, you can check the number of bytes contained in the jsonResults.length data jsonResults.length
When to use the if != nil ?
Consider the following example:
func toJson() -> NSData? { return nil
Beware: since jsonResults is a constant (aka let ) of its read-only variable, and you cannot change it in the future, even if this value got nil
Link
extension NSData {
source share