Вы путаете значения и типы. У вас есть только два типа: темы и подтемы. Итак, давайте сделаем их:
struct Topic {
let name: String
let description: String
let subTopics: [SubTopic]
}
struct SubTopic {
let title: String
let description: String
let content: [String]
}
Все остальное является просто экземпляром одного из этих двух типов:
let topics = [
Topic(
name: "Topic 1",
description: "Topic 1 description here.",
subTopics: [
SubTopic(
title: "Subtopic title here.",
description: "Some lorem ipsum description...",
content: ["First Sentence", "Second Sentence", "Hello"]
),
SubTopic(
title: "Subtopic title here.",
description: "Some lorem ipsum description...",
content: ["First Sentence", "Second Sentence", "Hello"]
),
]
),
Topic(
name: "Topic 2",
description: "Topic 2 description here.",
subTopics: [
SubTopic(
title: "Subtopic title here.",
description: "Some lorem ipsum description...",
content: ["First Sentence", "Second Sentence", "Hello"]
),
]
),
]
Поскольку это просто обычные массивы, вы можете легко их перебирать:
for topic in topics {
print("\(topic.name) - \(topic.description)")
print()
for subTopic in topic.subTopics {
let content = subTopic.content.joined(separator: "\n\t")
print("""
\(subTopic.title) - \(subTopic.description)
\(content)
""")
}
}
Выход:
Topic 1 - Topic 1 description here.
Subtopic title here. - Some lorem ipsum description...
First Sentence
Second Sentence
Hello
Subtopic title here. - Some lorem ipsum description...
First Sentence
Second Sentence
Hello
Topic 2 - Topic 2 description here.
Subtopic title here. - Some lorem ipsum description...
First Sentence
Second Sentence
Hello