13일차: SwiftUI의 100일
                                            
                                                
                                                
                                                
                                                
                                                
                                                 6244 단어  100daysofcodeswiftuiswift
                    
프로토콜, 확장 및 체크포인트 8
https://www.hackingwithswift.com/100/swiftui/13
프로토콜
Protocols는 데이터 유형이 지원할 것으로 예상되는 메서드 및 속성의 청사진을 정의합니다. 프로토콜은 class , struct 또는 enum 에 의해 채택되거나 준수될 수 있습니다. 그런 다음 프로토콜은 클래스, 구조 또는 열거형에 의해 채택될 수 있습니다.protocol Vehicle {
    var name: String { get }
    var currentPassengers: Int { get set }
    func estimateTime(for distance: Int) -> Int
    func travel(distance: Int)
}
struct Car: Vehicle {
    let name = "Car"
    var currentPassengers = 1
    func estimateTime(for distance: Int) -> Int {
        distance / 50
    }
    func travel(distance: Int) {
        print("I'm driving \(distance)km.")
    }
    func openSunroof() {
        print("It's a nice day!")
    }
}
func commute(distance: Int, using vehicle: Vehicle) {
    if vehicle.estimateTime(for: distance) > 100 {
        print("That's too slow! I'll try a different vehicle.")
    } else {
        vehicle.travel(distance: distance)
    }
}
let car = Car()
commute(distance: 100, using: car)
확장 프로그램
확장 기능을 사용하면 우리가 생성했든 다른 사람이 생성했든 관계없이 모든 유형에 기능을 추가할 수 있습니다. 심지어 Apple 자체 유형 중 하나일 수도 있습니다.
extension String {
    func trimmed() -> String {
        self.trimmingCharacters(in: .whitespacesAndNewlines)
    }
}
var quote = "   The truth is rarely pure and never simple   "
let trimmed = quote.trimmed()
체크포인트 8
건물을 설명하는 프로토콜을 만들고 다양한 속성과 메서드를 추가한 다음 이를 준수하는 두 개의 구조체 House와 Office를 만듭니다. 프로토콜에는 다음이 필요합니다.
해결책
Reference
이 문제에 관하여(13일차: SwiftUI의 100일), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/johnkevinlosito/day-13-100-days-of-swiftui-4994텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)