HarryHuang HarryHuang - 1 month ago 10
Swift Question

Swift2.3: NSMutableDictionary store integer from sqlite

Actually, I have no idea what is NSMutableDictionary, but it seems I need it

By the way, I use xcode 8 with swift 2.3.

I am adding a integer to this arry from sqlite, but it's error
The other are all the string, so they are not problem here.

Then, the error is here

http://imgur.com/a/Gbrp0

let recipeIsFavor = sqlite3_column_int(statement, 3)
let recipe_isFavor = Int.fromCString(UnsafePointer<CInt>(recipeIsFavor)) //<--------I wnat to use int here, but I have no idea.


What should I do to edit this code? I hope this error can disappear.

func loadData() {

let db_path = NSBundle.mainBundle().pathForResource("Recipes", ofType: "db")
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"
if(sqlite3_prepare_v2(db , query_stmt, -1, &statement, nil) == SQLITE_OK) {
self.data.removeAllObjects()
while (sqlite3_step(statement) == SQLITE_ROW) {
let recipeArray = NSMutableDictionary()

let recipeName = sqlite3_column_text(statement, 0)
let recipe_name = String.fromCString( UnsafePointer<CChar>(recipeName))

let recipeType = sqlite3_column_text(statement, 1)
let recipe_type.......

let recipeImage = sqlite3_column_text(statement, 2)
let recipe_image.......

let recipeIsFavor = sqlite3_column_int(statement, 3)
let recipe_isFavor = Int.fromCString(UnsafePointer<CInt>(recipeIsFavor)) //<--------I wnat to use int here, but I have no idea.

let recipeUserPhoto = sqlite3_column_text(statement, 4)
let recipe_userPhoto....

let recipeUserName = sqlite3_column_text(statement, 5)
let recipe_userName.......

recipeArray.setObject(recipe_name!, forKey: "recipeName")
recipeArray.setObject(recipe_type!, forKey: "recipeType")
recipeArray.setObject(recipe_image!, forKey: "recipeImage")
recipeArray.setObject(recipe_isFavor!, forKey: "recipIsFavor")
recipeArray.setObject(recipe_userPhoto!, forKey: "recipUserPhoto")
recipeArray.setObject(recipe_userName!, forKey: "recipUserName")

data.addObject(recipeArray)
}

sqlite3_finalize(statement)
}else {
print("read the sqlite data failed")
}

}

Answer

sqlite3_column_int() returns a "C integer" which is Int32 in Swift. To convert that to an Int, use just

let recipeIsFavor = sqlite3_column_int(statement, 3)
let recipe_isFavor = Int(recipeIsFavor)

Instead of NSMutableDictionary and NSMutableArray you can also use an array of Swift dictionaries of type [String: AnyObject] (or [String: Any] in Swift 3), then it would look like this:

var data: [[String: AnyObject]] = []

let query_stmt = "SELECT * FROM recipe"
if sqlite3_prepare_v2(db , query_stmt, -1, &statement, nil) == SQLITE_OK {
    data = []
    while (sqlite3_step(statement) == SQLITE_ROW) {
        var dict: [String: AnyObject] = [:]

        let recipeName = sqlite3_column_text(statement, 0)
        let recipe_name = String.fromCString(UnsafePointer<CChar>(recipeName)) ?? ""

        let recipeIsFavor = sqlite3_column_int(statement, 3)
        let recipe_isFavor = Int(recipeIsFavor)

        // ...

        dict["recipeName"] = recipe_name
        dict["recipIsFavor"] = recipe_isFavor
        // ...

        data.append(dict)
    }
    sqlite3_finalize(statement)
} else {
    print("read the sqlite data failed")
}
Comments