使用 SQLite 的单例错误 Swift

Error in Singleton usng SQLite Swift

当我尝试使用单例在 swift 中获取 SQLite 数据库中的所有数据时,我在 sharedInstance 中遇到错误,并且获取了零个结果。

这是我的代码:

DatabaseManager.swift

    import SQLite

class DatabaseManager {


    let shareInstance = DatabaseManager()
    let recipeTable = Table("recipes")
    let id = Expression<String>("id")
    let title = Expression<String>("title")
    let category = Expression<String>("category")
    let recipe = Expression<String>("recipe")
    let bookmark = Expression<String>("Bookmark")

    let db: Connection = {
        let path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
        return try! Connection("\(path)/recipe.sqlite")
    }()

    func findAll() -> Array<foodRecipe>{
        var foodRecipeArray = [foodRecipe]()

        do{
        //for row in try db.prepare(recipeTable.select(id,title,recipe,category,bookmark).filter(category == "appetizers")){
        for row in try shareInstance.db.prepare(recipeTable){
            let fd = foodRecipe()
            fd.id = row[id]
            fd.title = row[title]
            fd.recipe = row[recipe]
            fd.category = row[category]
            fd.bookmark = row[bookmark]
            foodRecipeArray.append(fd)
        }

        }
        catch let error as NSError {
            print("Error: \(error.description)")
        }
        return foodRecipeArray
    }
}

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    var foodRecipeArray: [foodRecipe]

    let dbMngr: DatabaseManager = DatabaseManager()

    foodRecipeArray = dbMngr.findAll()

    print("Food Recipe Array Count: \(foodRecipeArray.count)") //return 0


    print("Food Recipe Array: \(foodRecipeArray)") //result nil 


    // Override point for customization after application launch.
    return true


}

用作单例的共享实例必须static

static let shareInstance = DatabaseManager()