100 days of swiftui: checkpoint 8
https://www.hackingwithswift.com/quick-start/beginners/checkpoint-8
Your challenge is this: make a protocol that describes a building, adding various properties and methods, then create two structs, House and Office, that conform to it. Your protocol should require the following:
protocol Building {
var name: String { get set }
var rooms: Int { get set }
var cost: Int { get set }
var estateAgent: String { get set }
func printSummary()
}
extension Building {
func printSummary() {
let summary = """
\(name):
- has \(rooms) rooms.
- costs \(cost).
- \(estateAgent) is responsible for selling here.
"""
print(summary)
}
}
struct House: Building {
var name: String
var rooms: Int
var cost: Int
var estateAgent: String
}
struct Office: Building {
var name: String
var rooms: Int
var cost: Int
var estateAgent: String
}
var greenHouse = House(name: "green house", rooms: 3, cost: 100_000, estateAgent: "Ms. Green")
var smallOffice = Office(name: "small office", rooms: 2, cost: 1_000_000, estateAgent: "Some Boss")
greenHouse.printSummary()
smallOffice.printSummary()
결과:
green house:
- has 3 rooms.
- costs 100000.
- Ms. Green is responsible for selling here.
small office:
- has 2 rooms.
- costs 1000000.
- Some Boss is responsible for selling here.
코드 파일
https://github.com/soaringwave/Ios-studying/commit/e3b26c399cc4ccaf5bb4885f74900e9edafaea4f