as per Leo Dabus's excellent advice here How to insert into Sqlite with optional parameters using Swift 3 , I modified my class to allow optional parameters. However, I have a convenience method (as shown below) that accepts a JSON string to initialize the object. However, if you look at the self.init at the very bottom, it requires me to add "!" to the optional parameters. As I understand, this is not correct because these parameters can be nil. Is there a good way to handle this? Thanks!!!
class Address : BaseEntity {
var Id: Int
var AddressType: Int
var AddressStatus: Int
var Address1: String
var Address2: String
var City: String
var State: String
var Zip: String
var Country: String
var Latitude: Double
var Longitude: Double
init(id: Int, addressType: Int, addressStatus: Int, address1: String = "", address2: String = "", city: String = "", state: String = "", zip: String = "", country: String = "", latitude: Double = 0, longitude: Double = 0, isDeleted: Bool, created: Date? = nil, createdBy: Int = 0, modified: Date? = nil, modifiedBy: Int = 0) {
self.Id = id
self.AddressType = addressType
self.AddressStatus = addressStatus
self.Address1 = address1
self.Address2 = address2
self.City = city
self.State = state
self.Zip = zip
self.Country = country
self.Latitude = latitude
self.Longitude = longitude
super.init(isDeleted: isDeleted, created: created, createdBy: createdBy, modified: modified, modifiedBy: modifiedBy)
}
convenience init?(json: [String: Any]) {
guard let id = json["Id"] as? Int,
let addressType = json["AddressType"] as? Int,
let addressStatus = json["AddressStatus"] as? Int,
let isDeleted = json["IsDeleted"] as? Bool
else {
return nil
}
let address1 = json["Address1"] as? String
let address2 = json["Address2"] as? String
let city = json["City"] as? String
let state = json["State"] as? String
let zip = json["Zip"] as? String
let country = json["Country"] as? String
let latitude = json["Latitude"] as? Double
let longitude = json["Longitude"] as? Double
let date = Foundation.Date()
let created = date.dateFromJson(json: json["Created"] as? String)
let createdBy = json["CreatedBy"] as? Int
let modified = date.dateFromJson(json: json["Modified"] as? String)
let modifiedBy = json["ModifiedBy"] as? Int
self.init(id: id, addressType: addressType, addressStatus: addressStatus, address1: address1!, address2: address2!, city: city!, state: state!, zip: zip!, country: country!, latitude: latitude!, longitude: longitude!, isDeleted: isDeleted, created: created, createdBy: createdBy!, modified: modified, modifiedBy: modifiedBy!)
}
}