title: How to initialize class with optional properties in Swift 3
body: <p>I get a web API response, which I loop through and initialize a Customer class for each JSON object in the array. The Customer class has a base class with a couple optional properties. I get errors when there are null values in the JSON, and I am not sure how to handle them properly. I am especially unsure of how to handle a null Date in swift. If anyone could give me some advice, I would really appreciate it!</p>
  
  <p>JSON returned from Web API:</p>
  
  <pre><code>[{"Id":1,"BusinessId":1,"CompanyName":"Test Company4","FirstName":"Mike","LastName":"Doe","CustomerType":1,"CustomerStatus":1,"IsDeleted":false,"Created":"2016-12-22T20:12:10.2760144Z","CreatedBy":1,"Modified":"2016-12-29T19:29:26.1245219Z","ModifiedBy":1},{"Id":2,"BusinessId":1,"CompanyName":"Test Company5","FirstName":"Mike","LastName":"Doe","CustomerType":1,"CustomerStatus":1,"IsDeleted":false,"Created":"2017-01-03T20:18:51.7639708Z","CreatedBy":1,"Modified":null,"ModifiedBy":null}]
  </code></pre>
  
  <p>Customer class:</p>
  
  <pre><code>class Customer : BaseEntity {
  
      var Id: Int64
      var BusinessId: Int64
      var CustomerType: Int64
      var CustomerStatus: Int64
      var CompanyName: String
      var FirstName: String
      var LastName: String
  
      init?(json: [String: Any]) {
  
          let formatter = DateFormatter()
          formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
  
          guard let id = json["Id"] as? Int64,
              let businessId = json["BusinessId"] as? Int64,
              let customerType = json["CustomerType"] as? Int64,
              let customerStatus = json["CustomerStatus"] as? Int64,
              let companyName = json["CompanyName"] as? String,
              let firstName = json["FirstName"] as? String,
              let lastName = json["LastName"] as? String,
              let isDeleted = json["IsDeleted"] as? Bool
              else {
                  return nil
          }
  
          var created = Date()
          if let cdate = json["Created"] as? String {
              created = formatter.date(from: cdate)!
          }
          let createdBy = json["CreatedBy"] as? String
  
          var modified = Date()
          if let mdate = json["Modified"] as? String {
              modified = formatter.date(from: mdate)!
          }
          let modifiedBy = json["ModifiedBy"] as? String
  
          self.Id = id
          self.BusinessId = businessId
          self.CustomerType = customerType
          self.CustomerStatus = customerStatus
          self.CompanyName = companyName
          self.FirstName = firstName
          self.LastName = lastName
          super.init(isDeleted: isDeleted, created: created, createdBy: createdBy, modified: modified, modifiedBy: modifiedBy)
      }
  }
  </code></pre>
  
  <p>BaseEntity class that contains some optional properties:</p>
  
  <pre><code> class BaseEntity {
  
      var IsDeleted: Bool
      var Created: Date
      var CreatedBy: String
      var Modified: Date?
      var ModifiedBy: String?
  
      init(isDeleted: Bool, created: Date, createdBy: String, modified: Date, modifiedBy: String) {
          self.IsDeleted = isDeleted
          self.Created = created
          self.CreatedBy = createdBy
          self.Modified = modified
          self.ModifiedBy = modifiedBy
      }
   }
  </code></pre>
  
  <p>One of the problems I am running into is when I initialize Customer, my Customer object looks like this. Then when I try to insert it into a database, it does not recognize the BaseEntity properties as properties of Customer:</p>
  
  <pre><code>Id = 1
  BusinessId = 1
  CustomerType = 1
  CustomerStatus = 1
  CompanyName = "Test Company4"
  FirstName = "Mike"
  LastName = "Doe"
  BaseEntity
      IsDeleted = true
      Created = '1/1/2017'
      CreatedBy = 1
      Modified = '1/1/2017'
      ModifiedBy = 1
  </code></pre>