简体   繁体   中英

What is difference between '&' and ',' in swift about Protocol Composition

I have a question

In swift,

class Card : Equatable & CustomStringConvertible

and

class Card : Equatable, CustomStringConvertible

I think there are doing the same things. What is different??

From the Swift documentation on protocols , I think & is mainly used when you are requiring a variable to adopt to multiple protocols, ex:

func wishHappyBirthday(to celebrator: Named & Aged) {
    print("Happy birthday, \(celebrator.name), you're \(celebrator.age)!")
}

And you use , when you want to adopt a custom struct/class to multiple protocols:

struct SomeStructure: FirstProtocol, AnotherProtocol {
    // structure definition goes here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM