データを格納するためにsqliteを使用しようとしています。Swift2.3:「致命的なエラー:インデックスが範囲外です」とのsqlite配列エラー
そして、私はちょうど、機能コード
if segmentControl.selectedIndex == 0 {
let recipe = recipes[indexPath.row]
let aux:AnyObject = data[indexPath.row]
let table_recipeName = aux["recipeName"]
cell.nameLabel.text = table_recipeName as? String
let table_recipeType = aux["recipeType"]
cell.typeLabel.text = table_recipeType as? String
let table_recipeImage = aux["recipeImage"]
cell.thumbnailImageView.image = UIImage(named: table_recipeImage as! String)
let table_recipeIsFavor = aux["recipeIsFavor"]
cell.favorIconImageView.hidden = !recipe.isFavor
let table_recipeUserPhoto = aux["recipeUserPhoto"]
cell.userPhoto.image = UIImage(named: table_recipeUserPhoto as! String)
let table_recipeUserName = aux["recipeUserName"]
cell.userName.text = table_recipeUserName as? String
}
とsqliteの機能上chengeはここ
//**********************SQLite********************************//
var statement = COpaquePointer()
var data: [[String: AnyObject]] = []
func loadData() {
let db_path = "/Users/huangpinquan/Desktop/Recipes/Recipes/Recipes.sqlite"
var db = COpaquePointer()
let status = sqlite3_open(db_path,&db)
if (status == SQLITE_OK) {
print("Open the sqlite success!\n")
}else {
print("Open the sqlite failed!\n")
}
let query_stmt = "SELECT * FROM recipe WHERE type:'麵食' "
//let query_stmt = "SELECT * FROM recipe"
if(sqlite3_prepare_v2(db , query_stmt, -1, &statement, nil) == SQLITE_OK) {
//self.data.removeAllObjects()
while (sqlite3_step(statement) == SQLITE_ROW) {
var recipeArray: [String: AnyObject] = [:]
let recipeName = sqlite3_column_text(statement, 0)
let recipe_name = String.fromCString(UnsafePointer<CChar>(recipeName)) ?? ""
let recipeType = sqlite3_column_text(statement, 1)
//....just like "statement, 0"
let recipeImage = sqlite3_column_text(statement, 2)
//....
let recipeIsFavor = sqlite3_column_int(statement, 3)
//....
let recipeUserPhoto = sqlite3_column_text(statement, 4)
//....
let recipeUserName = sqlite3_column_text(statement, 5)
//....
recipeArray["recipeName"] = recipe_name
recipeArray["recipeType"] = recipe_type
recipeArray["recipeImage"] = recipe_image
recipeArray["recipIsFavor"] = recipe_isFavor
recipeArray["recipeUserPhoto"] = recipe_userPhoto
recipeArray["recipeUserName"] = recipe_userName
data.append(recipeArray)
}
sqlite3_finalize(statement)
}else {
print("read the sqlite data failed")
}
}
が "FUNC loaddataの"
let query_stmt = "SELECT * FROM recipe WHERE type:'麵食' "
それを、このようなコードエラーが発生します
fatal error: Index out of range
しかし、このようなコードならば、それはあなたがしたい場合には、私の全体のコードです
let query_stmt = "SELECT * FROM recipe"
任意のエラーを持っていません。
https://gist.github.com/anonymous/79eb4b2506889929cfe92dfcab3731c4
私はこのエラーを修正するために何をすべき?
このエラーはどこにありますか?どの行? –
FYI - 決して 'SELECT * ...'を使用しないでください。選択したい列を明示的に列挙します。 – rmaddy
エラーは "let aux:anyObject = data [indexPath.row]" – HarryHuang