protocol

protocol Record {
    var wins: Int {get}
    var losses: Int {get}

    func winningpercent() -> Double
}

可以在拓展中实现协议

extension Record {
    var description: String {
        return String(format: "WINS:%d,LOSSES:%d",arguments:[wins,losses])
    }

    var gamePlayed: Int {
        retrun wins + losses
    }
}

可以定义另外一个协议,然后限定同时实现两个协议的实现,继承这个拓展

protocol Tieable{
    var ties: Int {get set}
}

extension Record where Self: Tieable {
    var gamePlayed: Int {
        retrun gane: {
            return wins + losses + ties
        }
    }
}

struct FootballRecord : Record,Tieable {
    var wins: Int
    var losses: Int
    var ties: Int
}

可以限定方法传入的参数必须符合bc两种(或其他)协议

func a (one:protocol) {
    ......
}

struct aa: b,c {
    .....
}

a(aa)

你可能感兴趣的:(protocol)