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.

0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

6 Comments

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!!
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..."
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.
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.