如何构建包含段落的可下载内容包?



我正在制作一个闪存卡应用程序。每个闪存卡具有以下内容 - 一个话题 - 一个问题 - 一个答案

答案可以是多个段落,例如一篇短文。

例如: 主题: 营养 问题:什么是古生物? 答:古饮食是一种低碳水化合物饮食。 它依靠特定的肉类和蔬菜作为饮食的主食。你不能在古生物上吃面包。

CSV 似乎不是一个选项,除非我将 替换为类似 ~~

该段也可能有引号。我希望能够下载一包抽认卡以供离线使用,因此仅从数据库中提取并不理想。

是否有一种好的格式/结构可以用来捆绑一包抽认卡,以便于在本地系统上下载/解析/保存?

您可以按如下方式表示数据:

struct Card: Codable {
let topic: String
let question: String
let answer: String
}

然后,如果您有一个数组let card = [Card]则可以使用JSONEncoder转换为 JSON,并使用 JSON 转换为CardJSONDecoder

let cards = [Card(topic: "Nutrition", question: "What is paleo?", answer: "Paleo is a low carb diet.nIt relies on specific meats and vegetables as the staple of the diet. You cannot eat bread on paleo.")]
let data = try JSONEncoder().encode(cards)
let string = String(data: data, encoding: .utf8)!
print(string)
// [{"topic":"Nutrition","question":"What is paleo?","answer":"Paleo is a low carb diet.nIt relies on specific meats and vegetables as the staple of the diet. You cannot eat bread on paleo."}]
let newCards = try JSONDecoder().decode([Card].self, from: data)

最新更新