简体   繁体   English

Swift-如何使协议既是类又需要采用某种类型

[英]Swift - how to make protocol to be both class and need to adopt certain type

I am trying to make protocol that need to have certain type using Self == MyCustomClass . 我正在尝试使用Self == MyCustomClass来使协议具有某种类型。 But this protocol is used as delegate type and weak constraint requires it to be also class-only protocol with P rotocolName : class 但是此协议用作委托类型,并且弱约束要求它也是具有P rotocolName : class协议rotocolName : class

I have tried something like this 我已经尝试过这样的事情

protocol MusicPlayerViewDelegate : class where Self == MusicPlayerController {
}

and then: 接着:

weak var delegate : MusicPlayerViewDelegate?

Your protocol needs to extends NSObjectProtocol to be able to use weak with it. 您的协议需要扩展NSObjectProtocol才能对其使用弱。 You can also try to make MusicPlayerController extend NSObject but I'm not sure it will work. 您也可以尝试使MusicPlayerController扩展NSObject,但我不确定它是否可以工作。

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

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