Sorry for my English. i think its not true, in exercise we must "For further practice, extend the Song struct below by adding a isLongerThan method. You can paste your code from page 6 as a starting point. Then use a loop to find the longest song in the array and print it to the console." So my code is
struct Song {
let title: String
let artist: String
let duration: Int
var formattedDuration: String {
let minutes = duration / 60
// The modulus (%) operator gives the remainder
let seconds = duration % 60
return "\(minutes)m \(seconds)s"
}
var formattedTitle: String {
return "\(title) by \(artist)"
}
var songInformationReImplement :String {
return "The song \(title) by \(artist) has \(duration) sec"
}
//adding a isLongerThan method
var durationCount: Int {
return duration
}
func isLongerThan(_ song: Song) -> Bool {
let songOne = durationCount
let songTwo = song.durationCount
return songOne > songTwo
}**
}
//Hint: use a variable to keep track of the longest song and initialize it to the first one in the array.
var longestSong = songs[0]
//And for..in statment
for i in 0...songs.count - 1 {
if songs[i] .isLongerThan(longestSong) {
longestSong = songs[i]
}
}
print (longestSong)
Topic:
Programming Languages
SubTopic:
Swift
Tags: