Reputation: 23
The code to insert records into a table was working fine before the latest update, but now is throwing up this error so I was wondering what I was doing wrong.
Example code for record insert:
Recipes.insert(Title <- "Chocolate Cake", Description <- "Rich and moist", CookTime <- 20, PictureURL <- "http://w2.fnstatic.co.uk/sites/default/files/pictures/articles/omg-chocolate-cake-7.jpg", VideoURL <- "https://www.youtube.com/watch?v=ZqMqTB7RSjo", Instructions <- "Prepare ingredients into bowl. Whisk for 20 mins, and pour into cake moulding tin. Place in oven at 200C for 15 minutes. Allow 10 mins to cool before icing with chocolate frosting.", Category <- "Desert", Ingredients <- "50g Flour, 200ml Milk, 2 large eggs, Choclate frosting", Favourited <- false)
Example for database setup:
import Foundation
import SQLite
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
let db = Database("\(path)/databasetest.sqlite3")
let Recipes = db["Recipes"]
let RecipeID = Expression<Int>("RecipeID")
let Title = Expression<String>("Title")
let Description = Expression<String>("Description")
let CookTime = Expression<Int>("CookTime")
let PictureURL = Expression<String>("PictureURL")
let VideoURL = Expression<String>("VideoURL")
let Instructions = Expression<String>("Instructions")
let Category = Expression<String>("Category")
let Ingredients = Expression<String>("Ingredients")
let Favourited = Expression<Bool>("Favourited")
func TableSetup() {
db.create(table: Recipes, ifNotExists: true) { t in
t.column(RecipeID, primaryKey: true)
t.column(Title)
t.column(Description)
t.column(CookTime)
t.column(PictureURL)
t.column(VideoURL)
t.column(Instructions)
t.column(Category)
t.column(Ingredients)
t.column(Favourited, defaultValue: false)
}
I am using stephencelis' SQLite.swift project. https://github.com/stephencelis/SQLite.swift
Upvotes: 2
Views: 815
Reputation: 4964
In previous versions of SQLite.swift, the insert
function had overloads, which—in Swift 1.1—could be disambiguated with a trailing ?
. Swift removed this functionality and its migrator will automatically remove the ?
for you, breaking SQLite.swift.
Solutions included:
Using !
to disambiguate (if the statement should always succeed and a crash is OK on failure):
Recipes.insert(Title <- "Chocolate Cake", …)!
Using if
–let
to disambiguate (and group success logic within the block):
if let rowid = Recipes.insert(Title <- "Chocolate Cake", …) {
// success logic
}
Calling a tuple member, rowid
or statement
:
Recipes.insert(Title <- "Chocolate Cake", …).rowid
This was a regular point of confusion and SQLite.swift eventually removed the overloads, which means the insert
can now be called without a need to disambiguate:
Recipes.insert(Title <- "Chocolate Cake", …)
Upvotes: 3