草庐IT

UIApplicationDelegate_Protocol

全部标签

swift - 在 Swift 中声明符合协议(protocol)的类

在objective-C中我们可以声明一个符合某些协议(protocol)的类@property(nonatomic,strong)ClassobjectRestorationClass;并且可以给它赋值作为someObject.objectRestorationClass=[SomeClassclass];问题是:我怎样才能在swift做到这一点?我尝试使用varobjectRestorationClass:UIObjectRestoration!和someObject.objectRestorationClass=SomeClass.self但它给了我错误'SomeClass.Ty

swift - 无法将数组成员向下转换为协议(protocol)类型

我有两个协议(protocol)Valid和Resetable以及一个类型为[Valid]的inputViews数组.一切正常。所以现在我有了我的Resetable协议(protocol):protocolResetable:class{funcreset()}现在,inputViews中的所有内容也都符合Resetable协议(protocol),所以我想做的基本上是遍历并调用reset()。如果我这样做,它将起作用:forinputininputViews{(inputas!Resetable).reset()}但是我扩展了Array如下:extensionArraywhereEl

Swift - 段错误,符合协议(protocol)

我有课classPresenter>:NSObject{overrideinit(){super.init()Layer.sharedInstance.service.addListener(self)}}我想让Presenter再符合一个Protocol并写一个扩展:extensionPresenter:OneMoreProtocol{funcdoSomething(){self.update()}}但这会出错:在为源文件发出IR时..../Presenter.swift(段错误)协议(protocol)看起来:@objcprotocolOneMoreProtocol:AnyObje

ios - 协议(protocol)扩展和子类

我想知道为什么下面的内容没有打印出我认为应该打印的内容。/*Fails*/protocolTheProtocol{funcupdate()}classA:TheProtocol{}classB:A{}extensionTheProtocol{funcupdate(){print("CalledupdatefromTheProtocol")}}extensionTheProtocolwhereSelf:B{funcupdate(){print("CalledupdatefromB")}}letinstanceB=B()instanceB.update()letinstanceBViaPr

Swift 协议(protocol)继承和协议(protocol)一致性问题

protocolBasePresenterProtocol:class{}protocolDashboardPresenterProtocol:BasePresenterProtocol{}finalclassDashboardPresenter{weakvarview:DashboardPresenterProtocol?init(){self.view=DashboardViewController()}functest(){print("Hello")}}extensionDashboardPresenter:DashboardViewProtocol{}protocolBase

Swift 协议(protocol)错误 : 'weak' cannot be applied to non-class type

Protocols和class-boundProtocols有什么区别,我们应该在Swift中使用哪一个?protocolA:class{...}protocolA{...}当协议(protocol)未定义为:class时尝试添加weak委托(delegate)时出现错误:protocolA{...}weakvardelegate:A给出错误:'weak'cannotbeappliedtonon-classtype或'weak'mustnotbeappliedtonon-class-bound'A';consideraddingaprotocolconformancethathasac

ios - `Protocol can only be used as a generic constraint because it has Self or associated type requirements` 问题

我正在尝试生成一个符合协议(protocol)Protocoling的ViewModel,该协议(protocol)是通用的,并且具有关联的类型。有几个ViewModel符合协议(protocol),所以我正在尝试为viewModel创建一个工厂。我在Swift中遇到了以下错误:Protocol只能用作泛型约束,因为它有Self或关联类型要求示例代码:protocolProtocoling{associatedtypemodulingTypevardata:modulingType{get}}enumMyTypes{casemyNamecasemyAddress}classNameVi

ios - 如何实现具有通用约束类型属性的 Swift 协议(protocol)?

我想要一个看起来像这样的协议(protocol):protocolReturnType{varreturnType:ImmutableMappable.Type{get}}实现协议(protocol)的枚举部分:extensionShimEndPoint:ReturnType{varreturnType:ImmutableMappable.Type{switchself{case.deAuthorize(_,_):returnEmptyResponse.selfcase.authorize(_,_):returnAuthorizeResponse.selfcase.step(_,_,_

ios - 使用 Objective-C 属性 getter 实现 Swift 协议(protocol)

我想为Swift中所有可突出显示的View创建一个共同的祖先。我希望已经存在的实现highlighted属性的UIKit类开箱即用,所以在阅读thisanswer之后并检查theObjective-CgetterisdefinedasisHighlighted我将协议(protocol)定义更改为:@objcprotocolHighlightable{varhighlighted:Bool{@objc(isHighlighted)getset}}所以UILabel和UIControl的协议(protocol)实现就这么简单:extensionUILabel:Highlightable{

arrays - Swift 4.1 中对类绑定(bind)协议(protocol)的弱引用的通用数组

我正在尝试创建一个可以放入数组中的通用WeakReference类型(并最终创建一个通用的弱数组类型)。到目前为止一切顺利,但是下面的代码:classWeakReference{weakvarelement:ElementType?init(_element:ElementType){self.element=element}}protocolElement:AnyObject{}classWeakElementHolder{varweakElements:[WeakReference]=[]}产生这个编译器错误:WeakReference.swift:12:21:error:'Wea