2

将记录插入表的代码在最新更新之前工作正常,但现在抛出此错误,所以我想知道我做错了什么。

记录插入的示例代码:

   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)

数据库设置示例:

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)
}

我正在使用斯蒂芬斯的 SQLite.swift 项目。https://github.com/stephencelis/SQLite.swift

4

1 回答 1

3

在 SQLite.swift 的早期版本中,该insert函数具有重载,在 Swift 1.1 中,可以使用结尾的?. Swift 删除了这个功能,它的迁移器会自动?为你删除,破坏 SQLite.swift。

解决方案包括:

  1. 用于消除歧义!(如果语句应该总是成功并且失败时崩溃是可以的):

    Recipes.insert(Title <- "Chocolate Cake", …)!
    
  2. 使用if–<code>let 消除歧义(并在块内分组成功逻辑):

    if let rowid = Recipes.insert(Title <- "Chocolate Cake", …) {
        // success logic
    }
    
  3. 调用元组成员,rowidstatement

    Recipes.insert(Title <- "Chocolate Cake", …).rowid
    

这是一个常见的混淆点,SQLite.swift 最终删除了重载,这意味着insert现在可以调用而不需要消除歧义:

Recipes.insert(Title <- "Chocolate Cake", …)
于 2015-04-27T13:32:59.667 回答