Usage of structure in a function with «Value of type 'String' has no member 'artist'»

I'm trying to do the last questions. However, I'm having much difficulty with the last question and I'm unsure of my answers on the previous questions. Structure is actually a new concept for me.

let songTitles = ["Ooh yeah", "Maybe", "No, no, no", "Makin' up your mind"]

let artists = ["Brenda and the Del-chords", "Brenda and the Del-chords", "Fizz", "Boom!"]

let durations = [90, 200, 150, 440]



func songInformation(songTitles: String, artists: String, durations: Int) -> String {

   return "\(songTitles) by \(artists), duration \(durations)"

}



for i in 0 ... songTitles.count - 1 {

    print(songInformation(songTitles: songTitles[i], artists: artists[i], durations: durations[i]))

}

/*:

 The code above is prone to all sorts of errors. What would happen if your song catalog expanded but you forgot to update one of the three arrays? What if you added star rating data with a new array but forgot to modify the `songInformation` function?

 

 This kind of code is also hard to maintain and read. What if you were tracking thirty properties about songs, instead of just a handful? What would the parameter list of the `songInformation` function look like?

 

 Next you'll perform the same task using a data abstraction: the `Song` struct from the previous page.

 */

struct Song {

    let title: String

    let artist: String

    let duration: Int

}

//:- callout(Exercise): Below, use the `Song` struct from the previous page to simplify your code.

/* Create the array of songs here */

let song = [songTitles]

/* Declare the songInformation function here */

func songInformation2(songTitles: String, artists: String, durations: Int) -> String {

   return "\(songTitles) by \(artists), duration \(durations)"

}



/* Write a for...in loop here */

for song in songTitles {

    songInformation2(songTitles: song.title, artists: song.artist, durations: song.duration)

}
Answered by Claude31 in 722935022

Here is the error:

for song in songTitles {

you use songTitles which is an array of String. So, song has no title property.

You should create an array of songs as [Song] and initialise it.

You can declare as var if you want to modify later

let songs : [Song] = [
           Song(title: songTitles[0], artist: artists[0], duration: durations[0]),
           Song(title: songTitles[1], artist: artists[1], duration: durations[1]),
           Song(title: songTitles[2], artist: artists[2], duration: durations[2]),
           Song(title: songTitles[3], artist: artists[3], duration: durations[3])
       ]

And use in the loop:

for song in songs {
    songInformation2(songTitles: song.title, artists: song.artist, durations: song.duration)
}

Note: you could populate directly the songs such as:

Song(title: "Ooh yeah", artist: "Brenda and the Del-chords", duration: 90)

Any idea ? @Claude31

Accepted Answer

Here is the error:

for song in songTitles {

you use songTitles which is an array of String. So, song has no title property.

You should create an array of songs as [Song] and initialise it.

You can declare as var if you want to modify later

let songs : [Song] = [
           Song(title: songTitles[0], artist: artists[0], duration: durations[0]),
           Song(title: songTitles[1], artist: artists[1], duration: durations[1]),
           Song(title: songTitles[2], artist: artists[2], duration: durations[2]),
           Song(title: songTitles[3], artist: artists[3], duration: durations[3])
       ]

And use in the loop:

for song in songs {
    songInformation2(songTitles: song.title, artists: song.artist, durations: song.duration)
}

Note: you could populate directly the songs such as:

Song(title: "Ooh yeah", artist: "Brenda and the Del-chords", duration: 90)

Merci, t'es un héros @Claude31

Usage of structure in a function with «Value of type 'String' has no member 'artist'»
 
 
Q