간단한 프로토콜 만들기
protocol Printer {
var name: String { get set }
func printdescription()
}
struct Employees: Printer {
var name: String
var age: Int
func printdescription() {
print("Description: \(name) \(age)")
}
}
struct Offices: Printer {
var name: String
var employees: Int
func printdescription() {
print("Description: \(name) \(employees)") // "Description: Mail 2"
}
}
let employee1 = Employees(name: "John", age: 32)
let office1 = Offices(name: "Mail", employees: 2)
var list: [Printer] = [employee1, office1]
for element in list {
element.printdescription()
}
'SwiftUI' 카테고리의 다른 글
Extensions (0) | 2023.11.06 |
---|---|
Swift Protocols (Equatable, Comparable, Hashable, Numeric, CaseIterable) (0) | 2023.11.05 |
Managing a Shared Resource Using a Singleton (0) | 2023.11.05 |
Dictionary (0) | 2023.11.05 |
Range (0) | 2023.11.05 |