lundi 29 juin 2015

Writing a Singleton in Swift for Squeal

Im using Squeal to access a Sqlite database for my app. I want to write it as a Singleton helper class, but I can't figure out how to create a class global version of db.

Heres the code for the whole file:

import Foundation

import Squeal

class DatabaseHelper {
    static let sharedInstance = DatabaseHelper()

    let databaseFile = "record_store.db"

    private init() {
        let db = Database(path:databaseFile)

        // Create the table
        db?.createTable(
            "Records",
            definitions: [
                "ID INTEGER PRIMARY KEY AUTOINCREMENT",
                "Artist TEXT",
                "Album TEXT",
                "Gnere TEXT",
                "Year TEXT",
                "Size TEXT",
                "Speed Text"
            ]
        )

        NSLog("Database Created")
    }

    func Insert() {
        NSLog("Inserting stuff")
    }

    func Delete() {
        NSLog("Deleting stuff")
    }
}

So how can I make that db accessible to the whole class? I tried declaring

let db

and

let db = Database(path:databaseFile)

but both throw errors that I don't know how to handle.

Aucun commentaire:

Enregistrer un commentaire