简体   繁体   English

关于协议组成的swift中'&'和','有什么区别

[英]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:从关于 协议的 Swift 文档中,我认为&主要用于当您需要一个变量采用多个协议时,例如:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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