Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Generics in Swift Generics and Protocols Extending Generic Types

1 Answer

Xavier D
PLUS
Xavier D
Courses Plus Student 5,840 Points
import Foundation

struct Weather
{
    let temperature: Double, humidity: Double, chanceOfRain: Double,

        icon: String,

        highTemperature: Double, lowTemperature: Double,

        sunrise: Date, sunset: Date
}

let current = Weather(temperature: 63, humidity: 0.36, chanceOfRain: 0.04,

                      icon: "Cloudy",

                      highTemperature: 67, lowTemperature: 50,

                      sunrise: Date(), sunset: Date())

print(current)

protocol PrettyPrintable { var prettyDescription: String { get } }

extension Weather: PrettyPrintable
{
    var prettyDescription: String
    {                                                                                                                   return
        "Temperature: \(temperature)\nHumidity: \(humidity)\nChance of Rain: \(chanceOfRain)\nIcon: \(icon)\nHigh Temperature: \(highTemperature)\nLow Temperature: \(lowTemperature)\nSunrise time: \(sunrise)\nSunset time: \(sunset)"
    }
}

print(current.prettyDescription)

let tuesday = Weather(temperature: 63, humidity: 0.36, chanceOfRain: 0.04,

                      icon: "Cloudy",

                      highTemperature: 67, lowTemperature: 50,

                      sunrise: Date(), sunset: Date())
let wednesday = Weather(temperature: 63, humidity: 0.36, chanceOfRain: 0.04,

                      icon: "Cloudy",

                      highTemperature: 67, lowTemperature: 50,

                      sunrise: Date(), sunset: Date())
let thursday = Weather(temperature: 63, humidity: 0.36, chanceOfRain: 0.04,

                      icon: "Cloudy",

                      highTemperature: 67, lowTemperature: 50,

                      sunrise: Date(), sunset: Date())

let week =
            [
                current,

                tuesday,
                wednesday,
                thursday
            ]

for day in week {print(day.prettyDescription)}

//extension Array where Element: PrettyPrintable
//{
//    var prettyDescription: String
//    {var
//                                                    output = ""
//        for (index, element) in self.enumerated(){  output += "\n\n*********\n\nIndex:\(index)\n\n\(element.prettyDescription)"}
//                                            return  output
//    }
//}
//print(week.prettyDescription)

let array = [1,2]

extension Sequence where Iterator.Element == Weather
{
        var prettyDescription: String
{       var
                                                    output = ""
        for (index, element) in self.enumerated(){  output += "\n\n*********\n\nIndex:\(index)\n\n\(element.prettyDescription)"}
                                            return  output
}
}

print(week.prettyDescription)

dump(current)
```swift