如何使用Swift 3插入具有可选参数的Sqlite [英] How to insert into Sqlite with optional parameters using Swift 3
问题描述
我的此类带有一些可选属性:
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?
}
我正在尝试插入Sqlite数据库,如下所示:
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
我还是Swift的新手,但据我所知,我不想使用'!'当值可以为零时,是正确的吗?
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.
推荐答案
使所有类的属性常量(用let声明),使其成为非可选的,并在所有Address属性参数中添加必需的init().顺便说一句,Swift约定以小写字母开始vars命名.注意:除非有要求,否则应使用Int而不是Int64.关于属性的可选性,您只需在初始化时为其指定默认值即可.顺便说一句,除非您需要使对象持久化(符合NSCoding),否则最好使用结构:
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
这篇关于如何使用Swift 3插入具有可选参数的Sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!