AssociatedType与Self.AssociatedType之间的区别
创始人
2024-09-20 19:01:09
0

将AssociatedType定义为类型的协议,使用关联类型(associatedtype)指定此协议的类型实现,使用Self.AssociatedType指定协议实现者的类型。

AssociatedType定义:

protocol SomeProtocol {
   associatedtype ItemType
   func append(_ item: ItemType)
}

struct Stack: SomeProtocol {
   // 用 T 替代 ItemType
   typealias ItemType = T
   var items = [ItemType]()
   mutating func push(_ item: ItemType) {
       items.append(item)
   }
   mutating func pop() -> ItemType {
       return items.removeLast()
   }
   // 遵循协议 实现使用另一种类型的方法
   mutating func append(_ item: Int) {
       self.push(item as! T)
   }
}

Self.AssociatedType定义:

protocol SomeProtocol {
   associatedtype ItemType
   func append(_ item: Self.ItemType)
}

struct Stack: SomeProtocol {
   var items = [T]()
   mutating func push(_ item: T) {
       items.append(item)
   }
   mutating func pop() -> T {
       return items.removeLast()
   }
   // 遵循协议 实现使用另一种类型的方法
   mutating func append(_ item: Int) {
       self.push(item as! T)
   }
}

相关内容

热门资讯

透视攻略!哈糖大菠萝辅助器,c... 透视攻略!哈糖大菠萝辅助器,cloudpoker怎么开挂(真是存在有挂)1、用户打开应用后不用登录就...
aapoker有外 挂!aap... 您好,aapoker有外 挂这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多...
透视总结!智星菠萝可以辅助吗,... 透视总结!智星菠萝可以辅助吗,pokemmo修改器手机版(确实存在有挂)1、透视总结!智星菠萝可以辅...
aapoker有外 挂!德州a... aapoker有外 挂!德州aa扑克平台,(AAPOKEr)总是真的有挂,详细教程(有挂详情);建议...
透视最新!德州局透视脚本下载安... 透视最新!德州局透视脚本下载安装最新版本,wepoker透视有吗(果然真的有挂);1、起透看视 德州...
aapoker透视辅助!aa ... 1、aapoker透视辅助!aa poker有没有挂,(Aapoker)真是真的是有挂,详细教程(有...
aapoker辅助工具存在!德... aapoker辅助工具存在!德州aa扑克透明挂,(AAPOKEr)都是真的是有挂,详细教程(有挂黑科...
透视软件!哈糖大菠萝能开挂吗,... 透视软件!哈糖大菠萝能开挂吗,聚星ai辅助工具收费多少(原来存在有挂);1、完成聚星ai辅助工具收费...
aapoker挂!aapoke... aapoker挂!aapoker软件app,(AAPoker)一贯是真的有挂,详细教程(有挂了解),...
透视好友!约局吧德州可以透视吗... 透视好友!约局吧德州可以透视吗,竞技联盟辅助(都是真的是有挂)1、不需要AI权限,帮助你快速的进行约...