ios - swift3使用try建立SQLite,抛出“Errors thrown from here are not handle”

查看:174
本文介绍了ios - swift3使用try建立SQLite,抛出“Errors thrown from here are not handle”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

按照stephencelis/SQLite.swift的文档,使用try后一直报错,是什么问题?

import SQLite

class CreateSQLiteDB{
    let Sqlite3Path = NSSearchPathForDirectoriesInDomains(
        .documentDirectory, .userDomainMask, true
        ).first! + "/db.sqlite3"
    
    func createSQlite() {
        let db = try Connection(Sqlite3Path)
        let users = Table("users")
        let id = Expression<Int64>("id")
        let name = Expression<String?>("name")
        let email = Expression<String>("email")
        
        try db.run(users.create { t in
            t.column(id, primaryKey: true)
            t.column(name)
            t.column(email, unique: true)
        })
        
    }
}

解决方案

先看一下抛出的异常吧,对症下药。
swift 中,try-catch 的用法:


do {
  try expression
} catch let error {
  print(error)
}

Errors thrown from here are not handle 翻译过来就是:这里抛出的异常并未处理

此外,还有 try? 及 try! 。建议稍微去了解一下。

这篇关于ios - swift3使用try建立SQLite,抛出“Errors thrown from here are not handle”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆