Ios Swift如何访问嵌套结构中的信息?
我有一个包含主题和子主题信息的大型嵌套结构数据文件Ios Swift如何访问嵌套结构中的信息?,ios,swift,struct,nested,Ios,Swift,Struct,Nested,我有一个包含主题和子主题信息的大型嵌套结构数据文件 struct Topic{ struct Topic1{ let name = "Topic 1" let description = "Topic 1 description here." struct SubtopicUniqueName1 { let title = "Subtopic title here." let subtopic
struct Topic{
struct Topic1{
let name = "Topic 1"
let description = "Topic 1 description here."
struct SubtopicUniqueName1 {
let title = "Subtopic title here."
let subtopicDescription = "Some lorem ipsum description...."
let subtopicContent = ["First Sentence", "Second Sentence", "Hello"]
}
struct SubtopicUniqueName2 {
let title = "Subtopic title here."
let subtopicDescription = "Some lorem ipsum description...."
let subtopicContent = ["First Sentence", "Second Sentence", "Hello"]
}
}
struct Topic2{
let name = "Topic 2"
let description = "Topic 2 description here."
struct SubtopicUniqueName3 {
let title = "Subtopic title here."
let subtopicDescription = "Some lorem ipsum description...."
let subtopicContent = ["First Sentence", "Second Sentence", "Hello"]
}
}
}
我想得到Topic1,Topic2。。。以此类推,因为我最终将有多达14个主题。当我尝试let allTopics=[Topic.Topic1,Topic.Topic2]
我想通过这样的方式获得信息
for i in allTopics{
print(i().name)
}
最后,由于子主题结构嵌套得更远,我会在主题中以类似的方式访问它们的内容吗 allTopics的类型是[Any],它隐藏特定类的成员 解决方案: 使用属性
名称
和您感兴趣的任何其他属性创建协议,并将其应用于应具有这些属性的结构,然后创建实现协议的对象集合。
可以迭代集合,并且无需强制转换即可访问协议的属性
let allTopics: [TopicName] = [Topic.Topic1(), Topic.Topic2()]
allTopics.forEach {
print($0)
}
protocol TopicName {
var name: String { get }
}
struct Topic{
struct Topic1: TopicName {
let name = "Topic 1"
let description = "Topic 1 description here."
struct SubtopicUniqueName1 {
let title = "Subtopic title here."
let subtopicDescription = "Some lorem ipsum description...."
let subtopicContent = ["First Sentence", "Second Sentence", "Hello"]
}
struct SubtopicUniqueName2 {
let title = "Subtopic title here."
let subtopicDescription = "Some lorem ipsum description...."
let subtopicContent = ["First Sentence", "Second Sentence", "Hello"]
}
}
struct Topic2: TopicName {
let name = "Topic 2"
let description = "Topic 2 description here."
struct SubtopicUniqueName3 {
let title = "Subtopic title here."
let subtopicDescription = "Some lorem ipsum description...."
let subtopicContent = ["First Sentence", "Second Sentence", "Hello"]
}
}
}
您混淆了值和类型。这里只有两种类型:主题和子主题。因此,让我们制作它们:
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
非常感谢,这就是我问题的答案。如果我想访问其中一个主题下所有子主题的信息,是否需要创建新协议?例如,Topic1,获取[SubtopicUniqueName1,SubtopicUniqueName2]的数组,并使用方法获取数据。因此,如果我想访问每个子主题的“title”,我会将var title:String{get}添加到协议中,然后遍历我必须创建的适当数组,因为它嵌套得更深?@geistmate yes,没错,这是子弹孔上的绷带。当然,你可以在一个协议下统一所有这些不同的类型,但这并不能回答为什么首先有这么多类型的问题……但这也不是一个好主意。想象一下,每当你想打开一个不同的网页时,你都会买一台新电脑。那是毫无意义的。您将使用一台计算机,并访问多个页面。类似地,您应该只创建一些有意义的类型(
主题
,子主题
,尽管它们看起来完全相同),并创建这些类型的多个不同实例,每个实例都具有您想要的值。从另一篇文章中的注释继续:对于我尝试执行的任务,这无疑是一个更好的解决方案。从这里,我可以轻松地将这些数据传递给我的视图控制器。关于计算机和网页的类比也很好,它帮助我更好地理解。非常感谢您的帮助。@geistmate嘿,这回答了您的问题吗?是的,先生,回答了。非常感谢。仅标记为答案。:)