I'm creating a Puzzle Word project in Xcode 8 and Swift 3, I created SQLite file and plist file, the SQLite file has a 3 columns id, questions, and answers, and all these columns are (string), of course except id it's a integer, and my plist file is array of characters letters, my problem is How to convert column number 3 (answers) from string to array character like plist without quotation marks and square brackets?! This is my code connecting with SQLite Database (DatabaseHelper)
import GRDB
class DatabaseHelper {
var dbQueue: DatabaseQueue = {
var db: DatabaseQueue = DatabaseQueue()
do {
db = try DatabaseQueue(path: Bundle.main.path(forResource: "testDB", ofType: "sqlite")!)
print("Connect database sqlite is OK...")
} catch {
print("Connect databse fail !!!")
}
return db
}()
func getDatabase(rowId: Int) -> [DatabaseModel] {
var listDatabase = [DatabaseModel]()
dbQueue.inDatabase { db in
do {
for row in try Row.fetchAll(db, "select * from QuestionTable where Id = \(rowId)") {
let newDatabase = DatabaseModel()
newDatabase.id = row.value(named: "Id") as Int
newDatabase.ques = row.value(named: "question") as String
newDatabase.ans = row.value(named: "answer") as String
listDatabase.append(newDatabase)
}
} catch {
print("Get all database fail !!!")
}
}
return listDatabase
}
}
And this code is DatabaseModel:
import Foundation
class DatabaseModel: NSObject {
var id: Int?
var ques: String?
var ans = String()
}
This code calling from database :
var dbHelpr = DatabaseHelper()
var listdata = [DatabaseModel]()
let pathRes = Bundle.main.path(forResource: fileName, ofType: fileExt)
let pathDict = NSDictionary(contentsOfFile: pathRes!)
var letters : [String] = pathDict?.object(forKey: "Letters") as! [String]
listdata = dbHelpr.getDatabase(rowId: id)
// print("\(letters.shuffled())")
for data in listdata {
let dataAnswer : String = data.ans
let dataArrayAnswer = Array(dataAnswer.characters)
print(dataAnswer)
let lastLetters = letters.append(String(describing: dataArrayAnswer))
print(lastLetters)
}
And this my output in console :