Swift2.3:来自sqlite的NSMutableDictionary存储整数 [英] Swift2.3: NSMutableDictionary store integer from sqlite
问题描述
实际上,我不知道什么是NSMutableDictionary,但似乎我需要它
Actually, I have no idea what is NSMutableDictionary, but it seems I need it
顺便说一句,我在Swift 2.3中使用了xcode 8.
By the way, I use xcode 8 with swift 2.3.
我正在从sqlite向此对象添加一个整数,但这是错误的 另一个都是字符串,所以在这里没问题.
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.
然后,错误在这里
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")
}
}
推荐答案
sqlite3_column_int()
返回一个"C整数",在Swift中为Int32
.
要将其转换为Int
,只需使用
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)
您可以代替NSMutableDictionary
和NSMutableArray
也使用类型的Swift字典数组
[String: AnyObject]
(或Swift 3中的[String: Any]
),
那么它会看起来像这样:
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")
}
这篇关于Swift2.3:来自sqlite的NSMutableDictionary存储整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!