gpt4 book ai didi

ios - 如何在结构内部调用结构?

转载 作者:搜寻专家 更新时间:2023-11-01 06:16:58 24 4
gpt4 key购买 nike

这是一个基本问题,我是 swift 的新手,这是一个关于结构数组的特定问题(Struct inside struct)

我正在尝试像这样输出(以标题作为部分的 TableView 结构):

enter image description here

我可以用字典来做,我正在尝试在结构中创建数组

结构:

struct StructSections {
var sectionName: String!
var channels: StructChannels
var collapsed: Bool!

init(sectionName: String, channels: StructChannels, collapsed: Bool = false) {
self.sectionName = sectionName
self.channels = channels
self.collapsed = collapsed
}
}

struct StructChannels{
var channelName: String!
var streamURL: String!
var imageURL: String!
}

然后我创建函数来加载数据

func CreateRadioData() {
var JakartaChannels: [StructChannels] = []
JakartaChannels = [
StructChannels(channelName: "Prambors 102.2 FM Jakarta", streamURL: "http://masima.rastream.com/masima-pramborsjakarta", imageURL: "PramborsJakarta"),
StructChannels(channelName: "I-Radio 89.6 FM Jakarta", streamURL: "http://mra.rastream.com/mra_iradio", imageURL: "IRadioJakarta")
]

var MedanChannels: [StructChannels] = []
MedanChannels = [
StructChannels(channelName: "KISS 105 FM Medan", streamURL: "http://live.kissfm-medan.com:8080/kissfm.mp3", imageURL: "KissFMMedan")
]



var Sections:[StructSections] = []
Sections = [
StructSections(sectionName: "Jakarta", channels: JakartaChannels),
StructSections(sectionName: "Medan", channels: MedanChannels)
]
}

我在这些代码中遇到了错误:

 StructSections(sectionName: "Jakarta", channels: JakartaChannels),
StructSections(sectionName: "Medan", channels: MedanChannels)

如何调用 StructSections 结构体中的 JakartaChannelsMedanChannels 结构体?

最佳答案

在此声明中,您传递的是 StructChannels 数组,但预计只有 StructChannels ref。

StructSections(sectionName: "Jakarta", channels: JakartaChannels)

因此,创建 StructSections 属性 channel 到 StructChannels 数组。

struct StructSections {
var sectionName: String!
var channels: [StructChannels]
var collapsed: Bool!

init(sectionName: String, channels: [StructChannels], collapsed: Bool = false) {
self.sectionName = sectionName
self.channels = channels
self.collapsed = collapsed
}

关于ios - 如何在结构内部调用结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42571514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com