1

I have this class with some optional properties:

class Address {
    var Id: Int64
    var AddressType: Int64
    var AddressStatus: Int64
    var Address1: String?
    var Address2: String?
    var City: String?
    var State: String?
    var Zip: String?
    var Country: String?
    var Latitude: Double?
    var Longitude: Double?
}

I am trying to insert into a Sqlite database, like this:

let insert = table.insert(or: .replace, Id <- item.Id, AddressType <- item.AddressType, AddressStatus <- item.AddressStatus, Address1 <- item.Address1?, Address2 <- item.Address2?, City <- item.City?, State <- item.State?, Zip <- item.Zip?, Country <- item.Country?, Latitude <- item.Latitude?, Longitude <- item.Longitude?)

But I get this build error:

Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?

If I use '!' it will build, but I get this error when I run:

unexpectedly found nil while unwrapping an Optional value

I am still new to Swift, but from what I know, I don't want to use '!' when the value can be nil, correct?

I'm not sure what I'm doing wrong here.

Primico
  • 2,143
  • 3
  • 24
  • 36

1 Answers1

2

Make all your class properties constants (declare with let) and make them non optional adding a required init() with all your Address properties parameters. BTW it is Swift convention to start your vars naming with a lowercase letter. Note: You should use Int instead of Int64 unless it is a requirement. Regarding the optionality of your properties you can just assign a default value for them at initialization time. Btw It is better to use a struct unless you need to make your object persist (NSCoding compliant):

struct Address {
    let id: Int
    let addressType: Int
    let addressStatus: Int
    let address1: String
    let address2: String
    let city: String
    let state: String
    let zip: String
    let country: String
    let latitude: Double?
    let longitude: Double?
    init(id: Int, addressType: Int, addressStatus: Int, address1: String = "", address2: String = "", city: String = "", state: String = "", zip: String = "", country: String = "", latitude: Double = nil, longitude: Double = nil) {
        self.id = id
        self.addressType = addressStatus
        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
    }
}

So you can create your new object Address as follow:

let address1 = Address(id: 1, addressType: 2, addressStatus: 3)
let address2 = Address(id: 2, addressType: 3, addressStatus: 4, address1: "Any Address", latitude: 22.0, longitude: 43.0)   // You don't need to add all parameters as long as you keep them in the same order as your initializer

address2.latitude   // 22.0
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • 1
    Thanks, that makes sense. BTW, my properties are capitalized only because I am keeping it consistent with the properties from a web API I am calling. Thanks again!! – Primico Jan 26 '17 at 05:07
  • BTW, my "Address" class actually inherits from anther class "BaseEntity" and that is why I was using class instead of struct. When I try struct, it gives an error about "inheritance from non-protocol type..." – Primico Jan 26 '17 at 15:42
  • And one last thing.. your example above doesn't have any "?" in the init.. however the only way I saw to allow a nil date is to do "created: Date? = nil" is this correct? Everything else made perfect sense to me. Thanks again. – Primico Jan 26 '17 at 15:44
  • Yes you can do that to optionals also to make it an optional initializer parameter. You can see the same behavior when using the DateComponents initializer – Leo Dabus Jan 26 '17 at 15:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134095/discussion-between-primico-and-leo-dabus). – Primico Jan 26 '17 at 15:57
  • @LeoDabus. I know you're struggling to collect points :) .. you may want this bounty man! http://stackoverflow.com/questions/41874682/subtle-cast-warning-when-using-sqlite-swift-binding-to-any Damned if I can figure it out... – Fattie Jan 28 '17 at 18:36