Q:

How to check nil for variables in swift and avoid app crashing? 

I'm having my string declared as,
var firstName = String()

and I'm assigning value from parsed JSON content like,
firstName = json["first_name"].stringValue

But sometimes, there might be empty values in the JSON response and the app is crashing, I read about guard statement and if statement to check empty values, but that requires the declaration format to be changed, couldn't find a right way to handle this error without changing the declaration format. 
since I have declared all the variables in my app with similar formats, changing that requires time, I'm in the verge of uploading my app, this is my first swift app, if my declaration format is wrong please answer why it is, can someone help me out of this? 

A:

Code as of Swift 4:
Keep in mind that when you are using ".stringValue", it is almost the same as using a "!"  which will force a crash on nil. 
    if let firstName = json["first_name"]as?  String {
        //do stuff like
        self.firstName = firstName
    }

This will unwrap it to where you can get at the value if it isn't null and can be a string. 
Guard let's are really good for this though as you can account for it in the beginning and you can assume that it is not optional for the entire scope. 
    guard let firstName = json["first_name"]as?  String else {return}
    self.firstName = firstName

In addition, you could always check for nulls in one line and assign a default value if a nil value occurs. 
    self.firstName = (json["first_name"]as?  String) ?? " Default String"

A:

You can use next statement: 
guard let firstName = json["first_name"].stringValue else { // Do sth if nil }
// Do sth if not nil

Or you could use statement, which you wrote, but you should check variable 
firstName like this:
guard firstName ! = nil else { // Do sth if nil }
// Do sth if not nil

Or
if firstName ! = nil { 
   // Do sth if not nil 
}

