草庐IT

Protocols

全部标签

调用 Swift 协议(protocol)扩展方法而不是子类中实现的方法

我遇到了以下代码中解释的问题(Swift3.1):protocolMyProtocol{funcmethodA()funcmethodB()}extensionMyProtocol{funcmethodA(){print("DefaultmethodA")}funcmethodB(){methodA()}}//Test1classBaseClass:MyProtocol{}classSubClass:BaseClass{funcmethodA(){print("SubClassmethodA")}}letobject1=SubClass()object1.methodB()////Te

调用 Swift 协议(protocol)扩展方法而不是子类中实现的方法

我遇到了以下代码中解释的问题(Swift3.1):protocolMyProtocol{funcmethodA()funcmethodB()}extensionMyProtocol{funcmethodA(){print("DefaultmethodA")}funcmethodB(){methodA()}}//Test1classBaseClass:MyProtocol{}classSubClass:BaseClass{funcmethodA(){print("SubClassmethodA")}}letobject1=SubClass()object1.methodB()////Te

swift - 为什么我不能将 Protocol.Type 传递给通用 T.Type 参数?

我正在使用Swinject并且有一个问题困扰着我。我几乎一整天都被困在这个问题上。我怀疑这是由于Swift是一种静态类型语言,但我并不完全确定。我在这个游乐场总结了我的问题protocolProtocol{}classClass:Protocol{}lettest:Protocol.Type=Class.selffuncprintType(confromingClassType:Protocol.Type){print(confromingClassType)}funcprintType(serviceType:Service.Type){print(serviceType)}prin

swift - 为什么我不能将 Protocol.Type 传递给通用 T.Type 参数?

我正在使用Swinject并且有一个问题困扰着我。我几乎一整天都被困在这个问题上。我怀疑这是由于Swift是一种静态类型语言,但我并不完全确定。我在这个游乐场总结了我的问题protocolProtocol{}classClass:Protocol{}lettest:Protocol.Type=Class.selffuncprintType(confromingClassType:Protocol.Type){print(confromingClassType)}funcprintType(serviceType:Service.Type){print(serviceType)}prin

swift - 使用协议(protocol)向现有枚举添加案例

我想创建一个protocol,它在所有符合此protocol的enums上强制执行特定情况。例如,如果我有一个像这样的enum:enumFoo{casebar(baz:String)casebaz(bar:String)}我想用添加另一种情况的协议(protocol)来扩展它:caseFuzz(Int)这可能吗? 最佳答案 设计解决方法是使用带有static变量的struct。注意:这是在Swift3中为Notification.Name所做的下面是Swift3的实现结构:structCar:RawRepresentable,Equ

swift - 使用协议(protocol)向现有枚举添加案例

我想创建一个protocol,它在所有符合此protocol的enums上强制执行特定情况。例如,如果我有一个像这样的enum:enumFoo{casebar(baz:String)casebaz(bar:String)}我想用添加另一种情况的协议(protocol)来扩展它:caseFuzz(Int)这可能吗? 最佳答案 设计解决方法是使用带有static变量的struct。注意:这是在Swift3中为Notification.Name所做的下面是Swift3的实现结构:structCar:RawRepresentable,Equ

Swift 协议(protocol)性能

这个问题涉及在Swift2.2中作为协议(protocol)扩展编写的函数的效率。有谁知道加快运行时间的方法吗?假设我有一个Int符合的协议(protocol)NumberprotocolNumber:Equatable,IntegerLiteralConvertible{init(_int:Int)init(_number:Self)func+(lhs:Self,rhs:Self)->Selffunc*(lhs:Self,rhs:Self)->Selffunc-(lhs:Self,rhs:Self)->Selffunc/(lhs:Self,rhs:Self)->Self}extens

Swift 协议(protocol)性能

这个问题涉及在Swift2.2中作为协议(protocol)扩展编写的函数的效率。有谁知道加快运行时间的方法吗?假设我有一个Int符合的协议(protocol)NumberprotocolNumber:Equatable,IntegerLiteralConvertible{init(_int:Int)init(_number:Self)func+(lhs:Self,rhs:Self)->Selffunc*(lhs:Self,rhs:Self)->Selffunc-(lhs:Self,rhs:Self)->Selffunc/(lhs:Self,rhs:Self)->Self}extens

swift - 协议(protocol)扩展中的 'where self' 是什么

我看到了很多格式如下的例子extensionProtocolnamewhereSelf:UIViewController协议(protocol)扩展中的whereSelf是什么。我找不到这方面的文档。 最佳答案 语法是:https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID521考

swift - 协议(protocol)扩展中的 'where self' 是什么

我看到了很多格式如下的例子extensionProtocolnamewhereSelf:UIViewController协议(protocol)扩展中的whereSelf是什么。我找不到这方面的文档。 最佳答案 语法是:https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID521考