简体   繁体   中英

Convert String to Array Character

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 :

enter image description here

let myString = "This is my answer"
let chars = Array(myString)

According to title of the question, I post this code as it meet the requirements to make array of characters from String.

So according to your code, you were almost there, but just remove the characters property of String because it already is Sequence :)

let dataArrayAnswer = Array(dataAnswer)

for char in dataArrayAnswer{
//Prints next char of whatever your string is...
print(char)
}

EDIT: Another approach could be this as it converts Character array into String array:

let dataArrayAnswer: [String] = dataAnswer.characters.map{String($0)}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM