Swift 3 - save json using struct model with null value
<p>I am working on consuming the restful web service which returns the data in JSON format. This is the Sample data : </p>

<pre><code>[
{
    "name": "Mark E",
    "categories": "process",
    "id": 1,
    "checkedOut": null,
    "checkedOutBy": null
},
{
    "name": "John",
    "categories": null,
    "id": 2,
    "checkedOut": null,
    "checkedOutBy": null
}
]
</code></pre>

<p>I am parsing this json using this code. Also I created struct model for this. </p>

<pre><code>let task = session.dataTask(with: url) { (data, response, error) in
        var myModel = [MyModel]()
        if let HTTPResponse = response as? HTTPURLResponse {
            let status = HTTPResponse.statusCode
            if(status == 200) {

                guard let data = data else {
                    print("No Data!!")
                    return completion(false, myModel)
                }
                guard let json = try! JSONSerialization.jsonObject(with: data, options: []) as? NSArray else {
                    print("Not an array")
                    return completion(false, myModel)
                }

                for jsondata in json {
                    guard let newdata = MyModel(json: jsondata) else {
                        continue
                    }
                    myModel.append(newdata)
                }
                completion(true,myModel)
            }else {
                completion(false,myModel)
            }
        }
    }
    task.resume()
</code></pre>

<p>And this is my data model struct  </p>

<pre><code>struct MyModel {
var name : String
var categories : String
var id : Int
var checkedOut : String
var checkedOutBy : String

init?(json:Any) {
    guard let myModel = json as? NSDictionary else {
        return nil
    }

    guard let name = myModel["name"] as? String,
          let id = myModel["id"] as? Int else {
            return nil
    }

    self.name = author
    self.id = id

   // This is how I am handling the null values. 
   if let categories = myModel["categories"] as? String {
        self.categories = categories
    } else {
        self.categories = ""
    }

    if let lastCheckedOut = myModel["lastCheckedOut"] as? String {
        self.lastCheckedOut = lastCheckedOut
    } else {
        self.lastCheckedOut = ""
    }

    if let lastCheckedOutBy = myModel["lastCheckedOutBy"] as? String {
        self.lastCheckedOutBy = lastCheckedOutBy
    }else {
        self.lastCheckedOutBy = ""
    }
  }
}
</code></pre>

<p>Here in <code>struct MyModel</code>, I am using <code>if let</code> for checking null values. Could anyone please suggest me this is the right way to check each variable for null? Is there any other way to do this?</p>

<p>If I check for null value using <code>guard</code>, its not adding any object into array.  </p>

<p>If the value is null then it simply assign empty char <code>""</code> to that variable and it will add into MyModel array of objects. </p>