繁体   English   中英

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

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

我有个问题

在迅速,

class Card : Equatable & CustomStringConvertible

class Card : Equatable, CustomStringConvertible

我认为正在做同样的事情。 有什么不同吗??

从关于 协议的 Swift 文档中,我认为&主要用于当您需要一个变量采用多个协议时,例如:

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

并使用,当你想通过一个自定义的结构/类多种协议:

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

暂无
暂无

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

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