How to insert into Sqlite with optional parameters using Swift 3

别来无恙 提交于 2019-12-08 06:38:40

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/41866593/how-to-insert-into-sqlite-with-optional-parameters-using-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!