Hendra Gunawan
Hendra Gunawan

Reputation: 63

How to make NSURL variable is nil when parsed from null String?

I'm a newbie in iOS development. Sometimes my API server returns a nil value. This is raising an error when I try to create a NSURL instance.

Here is the condition:

NSString *tmpURL = nil;
NSURL *url = [NSURL URLFromString:tmpURL];

In this condition, it will make the app crash. I just want to make the url variable nil, not raise an error.

Upvotes: 3

Views: 245

Answers (1)

Jelle
Jelle

Reputation: 1034

How about:

NSString *tmpURL = nil;
NSURL *url = tmpUrl ? [NSURL URLFromString:tmpURL] : nil;

Upvotes: 6

Related Questions