Reputation: 75
it's a basic question, I'm new to swift, and this is a specific question about Struct Arrays ( Struct inside struct)
I'm trying to have an output (TableView structure with header as section) like this:
I can do it with Dictionary, I'm trying to make arrays in struct
Structs:
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!
}
and then I create function to load data
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)
]
}
I got error in these codes:
StructSections(sectionName: "Jakarta", channels: JakartaChannels),
StructSections(sectionName: "Medan", channels: MedanChannels)
How to call the structs JakartaChannels
and MedanChannels
inside the struct StructSections
?
Upvotes: 1
Views: 892
Reputation: 9226
In this statement you are passing array of StructChannels
but it is expected only StructChannels
ref.
StructSections(sectionName: "Jakarta", channels: JakartaChannels)
so, create StructSections
property channels to array of 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
}
}
Upvotes: 4