这是我的数组的struct Question {
let imgName: String
let questionText: String
let options: [String]
let correctAns: Int
var wrongAns: Int
var isAnswered: Bool
}
var questionsArray = [Question]()
:
let que1 = Question(imgName: "img1", questionText: "What is 2 x 2 ?", options: ["2", "4", "8", "6"], correctAns: 1, wrongAns: -1, isAnswered: false)
let que2 = Question(imgName: "img2", questionText: "What is 4 + 2 ?", options: ["9", "4", "3", "6"], correctAns: 3, wrongAns: -1, isAnswered: false)
questionsArray = [que1, que2]
以下是当前填充数组的方式:
questionsArray
我想将数据放入文本文件中并填充Question(imgName: "img1", questionText: "What is 2 x 2 ?", options: ["2", "4", "8", "6"], correctAns: 1, wrongAns: -1, isAnswered: false)
Question(imgName: "img2", questionText: "What is 4 + 2 ?", options: ["9", "4", "3", "6"], correctAns: 3, wrongAns: -1, isAnswered: false)
。所以我创建了一个文件data.txt并将其放入bundle中。以下是data.txt的内容。每个问题都用一个新行分隔。
var arrayOfStrings: [String]?
do {
if let path = Bundle.main.path(forResource: "data", ofType: "txt") {
let data = try String(contentsOfFile:path, encoding: String.Encoding.utf8)
arrayOfStrings = data.components(separatedBy: "\n")
questionsArray = arrayOfStrings
}
} catch let err as NSError {
print(err)
}
我尝试使用这种方法:
Cannot assign value of type '[String]?' to type '[Question]'
但是,我收到了questionsArray = arrayOfStrings
行的错误{{1}}。
如何解决这个问题?
答案 0 :(得分:1)
这是一个更好的数据管理快速教程。它广泛用于您的代码。
粘贴此代码,它会在桌面上创建一个JSON文件questions.json
struct Question : Encodable {
let imgName: String
let questionText: String
let options: [String]
let correctAns: Int
var wrongAns: Int
var isAnswered: Bool
}
let que1 = Question(imgName: "img1", questionText: "What is 2 x 2 ?", options: ["2", "4", "8", "6"], correctAns: 1, wrongAns: -1, isAnswered: false)
let que2 = Question(imgName: "img2", questionText: "What is 4 + 2 ?", options: ["9", "4", "3", "6"], correctAns: 3, wrongAns: -1, isAnswered: false)
let questionsArray = [que1, que2]
do {
let jsonData = try JSONEncoder().encode(questionsArray)
let url = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Desktop/questions.json")
try jsonData.write(to: url)
} catch {
print(error)
}
将文件从桌面拖到项目导航器中。确保选中Copy if needed
。
在项目中使用此代码
struct Question : Decodable {
let imgName: String
let questionText: String
let options: [String]
let correctAns: Int
var wrongAns: Int
var isAnswered: Bool
}
var questionsArray = [Question]()
let url = Bundle.main.url(forResource: "questions", withExtension: "json")!
let data = try! Data(contentsOf: url)
questionsArray = try! JSONDecoder().decode([Question].self, from: data)
答案 1 :(得分:0)
将数据保存在文件中是个好主意。但是在txt的格式中,特别是在这种格式下,下面绝对不是一个好主意:
Question(imgName: "img1", questionText: "What is 2 x 2 ?", options: ["2", "4", "8", "6"], correctAns: 1, wrongAns: -1, isAnswered: false)
你应该使用非常适合这种情况的json格式。您将数据保存到file.json。
怎么做?将Codable协议添加到struct问题:
struct Question: Codable {
let imgName: String
let questionText: String
let options: [String]
let correctAns: Int
var wrongAns: Int
var isAnswered: Bool
}
然后编码数据:
do {
let encodedData = try JSONEncoder().encode(questionsArray)
// Write encoded data to file
} catch let err {
print(err)
}
当您想要检索数据时:
let data = Data(contentsOf: url)
do {
let decodedData = try JSONDecoder().decode([Question].self, from: data)
} catch let err {
// Failed to decode data
print(err)
}