使用SQLite Swift的Singleton中出现错误

MK开发

当我尝试使用单例快速获取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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Swift 3在do {行中出现预期的声明错误

来自分类Dev

使用nullptr在Codeblocks IDE中出现错误

来自分类Dev

使用ArrayLists的ListView中出现错误

来自分类Dev

使用ArrayLists的ListView中出现错误

来自分类Dev

使用sql的Android Studio中出现错误

来自分类Dev

在多个表中出现错误输出并加入sqlite

来自分类Dev

在sqlite中出现错误,如ionic1中的查询

来自分类Dev

使用 '。' 使用属性路由的Web Api中出现404错误

来自分类Dev

使用Slug访问url时在Django中出现NoReverseMatch错误

来自分类Dev

使用LaunchControl运行时,bash脚本中出现错误

来自分类Dev

使用BigDecimal会导致计算中出现轻微错误

来自分类Dev

不使用SignalR时ELMAH中出现SignalR错误

来自分类Dev

使用泛型时在变量分配中出现错误

来自分类Dev

使用泛型时在变量分配中出现错误

来自分类Dev

使用Hibernate:3.6.10.15插件的Grails 2.4.0中出现错误

来自分类Dev

使用BigDecimal会导致计算中出现轻微错误

来自分类Dev

使用Openlayers的WFS请求中出现奇怪的错误

来自分类Dev

使用geom_errorbar时数据帧中出现错误?

来自分类Dev

为什么在使用Eclipse的python中出现语法错误?

来自分类Dev

使用URL模板标记后,在Django中出现错误/ NoReverseMatch

来自分类Dev

使用 Visual Studio 在 emgu cv 中出现 foreach 循环错误

来自分类Dev

使用 IIS Express 在 AngularJS 项目中出现 403 错误

来自分类Dev

使用 if 语句的过程中出现未知列错误

来自分类Dev

使用Accelerate Framework在Swift中出现cblas_dgemm问题

来自分类Dev

Swift博客的Balloons Demo中出现错误

来自分类Dev

使用basichttpBinding和流的transfermode的WCF中出现错误的错误请求

来自分类Dev

在#Include指令中使用C文件时,C程序中出现错误(多个定义错误)

来自分类Dev

使用Turtle的Python中出现错误的颜色字符串错误

来自分类Dev

在phonegap中使用sqlite插件时出现Ajax错误

Related 相关文章

  1. 1

    使用Swift 3在do {行中出现预期的声明错误

  2. 2

    使用nullptr在Codeblocks IDE中出现错误

  3. 3

    使用ArrayLists的ListView中出现错误

  4. 4

    使用ArrayLists的ListView中出现错误

  5. 5

    使用sql的Android Studio中出现错误

  6. 6

    在多个表中出现错误输出并加入sqlite

  7. 7

    在sqlite中出现错误,如ionic1中的查询

  8. 8

    使用 '。' 使用属性路由的Web Api中出现404错误

  9. 9

    使用Slug访问url时在Django中出现NoReverseMatch错误

  10. 10

    使用LaunchControl运行时,bash脚本中出现错误

  11. 11

    使用BigDecimal会导致计算中出现轻微错误

  12. 12

    不使用SignalR时ELMAH中出现SignalR错误

  13. 13

    使用泛型时在变量分配中出现错误

  14. 14

    使用泛型时在变量分配中出现错误

  15. 15

    使用Hibernate:3.6.10.15插件的Grails 2.4.0中出现错误

  16. 16

    使用BigDecimal会导致计算中出现轻微错误

  17. 17

    使用Openlayers的WFS请求中出现奇怪的错误

  18. 18

    使用geom_errorbar时数据帧中出现错误?

  19. 19

    为什么在使用Eclipse的python中出现语法错误?

  20. 20

    使用URL模板标记后,在Django中出现错误/ NoReverseMatch

  21. 21

    使用 Visual Studio 在 emgu cv 中出现 foreach 循环错误

  22. 22

    使用 IIS Express 在 AngularJS 项目中出现 403 错误

  23. 23

    使用 if 语句的过程中出现未知列错误

  24. 24

    使用Accelerate Framework在Swift中出现cblas_dgemm问题

  25. 25

    Swift博客的Balloons Demo中出现错误

  26. 26

    使用basichttpBinding和流的transfermode的WCF中出现错误的错误请求

  27. 27

    在#Include指令中使用C文件时,C程序中出现错误(多个定义错误)

  28. 28

    使用Turtle的Python中出现错误的颜色字符串错误

  29. 29

    在phonegap中使用sqlite插件时出现Ajax错误

热门标签

归档