ios - Writing a Singleton in Swift for Squeal -


im using squeal access sqlite database app. want write singleton helper class, can't figure out how create class global version of db.

heres code 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 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 make db accessible whole class? tried declaring

let db 

and

let db = database(path:databasefile) 

but both throw errors don't know how handle.

@ali beadle had correct answer. wanted expand upon full example make more clear.

i've made databasefile constant private. don't think need other people accessing file name, actual database.

import foundation  import squeal  class databasehelper { static let sharedinstance = databasehelper()  private let databasefile = "record_store.db"  let db: database  private init() {     db = database(path:databasefile)      // create 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") } 

}


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -