dimanche 9 août 2015

SQLite.Swift: creating tables in AppDelegate, insert queries in ViewController

I have set up my classes for each table, including a create table function. I did this in my AppDelegate, ostensibly to make them available to all my view controllers.

I am now attempting to write my insert queries in the View Controller (which is where they would be run), but I'm getting some errors.

Here is my class in AppDelegate where the table is created:

class PCharacter {
    let PCharacters = db["PCharacters"]
    let id = Expression<Int64>("id")
    let charName = Expression<String>("charName")
    let currentHP = Expression<Int64>("currentHP")
    let maxHP = Expression<Int64>("maxHP")
    let currentMP = Expression<Int64>("currentMP")
    let maxMP = Expression<Int64>("maxMP")
    let currentExp = Expression<Int64>("currentExp")
    let nextExp = Expression<Int64>("nextExp")
    let charWeaponID = Expression<Int64>("charWeaponID")

func createTable() {
    db.create(table: PCharacters) { t in
        t.column(id, primaryKey: .Autoincrement)
        t.column(charName, unique: true)
        t.column(currentHP)
        t.column(maxHP)
        t.column(currentMP)
        t.column(maxMP)
        t.column(currentExp)
        t.column(nextExp)
        //t.foreignKey(charWeaponID, on: Weapon.Weapons[id], delete: .SetNull)
    }
}
}

And here is the update query in my ViewController:

AppDelegate.PCharacters.PCharacter.insert(AppDelegate.PCharacters.charName <- newCharName, 
AppDelegate.PCharacters.charHP <- newCharHP, AppDelegate.PCharacters.charMP <- newCharMP)

The error I'm receiving is "'AppDelegate.Type' does not have a member named 'PCharacters'".

Am I referencing the values from AppDelegate wrong? I'm still somewhat new to Swift, as well as to SQLite.Swift, so I apologize if this is a very basic question. ;-)

Aucun commentaire:

Enregistrer un commentaire