简体   繁体   English

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

[英]How to structure a downloadable content package that includes paragraphs?

I am making a flash cards app.我正在制作一个闪存卡应用程序。 Each flash card has the following - a topic - a question - an answer每张闪存卡都有以下内容 - 一个主题 - 一个问题 - 一个答案

The answer can be multiple paragraphs like a short article.答案可以是多个段落,如一篇短文。

For example: topic: nutrition question: What is paleo?例如:主题:营养问题:什么是古? answer: Paleo is a low carb diet.答案:Paleo 是一种低碳水化合物饮食。 \\n It relies on specific meats and vegetables as the staple of the diet. \\n 它依赖特定的肉类和蔬菜作为饮食的主食。 You cannot eat bread on paleo.你不能在古时吃面包。

CSV doesn't seem like an option unless I sub out the \\n for something like ~~ CSV 似乎不是一个选项,除非我将 \\n 分出类似 ~~

The paragraph may have quotes in it as well.段落中也可能有引号。 I would like to be able to download a pack of flashcards for offline use so just pulling from a database wouldn't be ideal for this.我希望能够下载一包抽认卡以供离线使用,因此仅从数据库中提取并不适合于此。

Is there a good format/structure I can use to bundle up a pack of flashcards for easy download/parsing/saving on the local system?有没有一种好的格式/结构可以用来捆绑一包抽认卡,以便在本地系统上轻松下载/解析/保存?

You could represent your data as follows:您可以按如下方式表示您的数据:

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

Then if you have an array let card = [Card] you can convert to JSON using a JSONEncoder and from JSON to Card using JSONDecoder然后,如果你有一个数组let card = [Card]您可以使用转换成JSON JSONEncoder和JSON到Card使用JSONDecoder

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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