我正在使用SQLite库,其中查询返回可选值以及可能抛出错误.我想有条件地解包该值,或者如果它返回错误则收到nil.我不完全确定怎么说这个,这段代码会解释,这就是它的样子:
func getSomething() throws -> Value? {
//example function from library, returns optional or throws errors
}
func myFunctionToGetSpecificDate() -> Date? {
if let specificValue = db!.getSomething() {
let returnedValue = specificValue!
// it says I need to force unwrap specificValue,
// shouldn't it be unwrapped already?
let specificDate = Date.init(timeIntervalSinceReferenceDate: TimeInterval(returnedValue))
return time
} else {
return nil
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法避免不得不强行打开那里?在更新到Swift3之前,我没有被迫在这里强行打开包装.
以下是实际代码.只是想从所有条目中获取最新的时间戳:
func getLastDateWithData() -> Date? {
if let max = try? db!.scalar(eventTable.select(timestamp.max)){
let time = Date.init(timeIntervalSinceReferenceDate: TimeInterval(max!))
// …Run Code Online (Sandbox Code Playgroud)