草庐IT

PROTOCOL

全部标签

ios - Swift 中任何协议(protocol)的通用约束

是否可以在Swift中限制泛型类型接受协议(protocol)?我已经实现了包含弱对象列表的包装器,我需要将其扩展到协议(protocol)。protocolIncrementable:class{funcinc()}classCounter:Incrementable{varn:Int=0funcinc(){n+=1}}structWeak{weakvarvalue:T?init(value:T?){self.value=value}}varcnt:Counter?=Counter()letcounters:[Weak]=[Weak(value:cnt),Weak(value:Cou

swift - 在 Swift 中约束一个泛型与另一个泛型

我遇到了一些协议(protocol)问题:protocolBaz{funcbar(input:T)}函数bar是通用的,因为我不希望协议(protocol)本身有一个Self(它需要在集合中可用)。我有一个定义为协议(protocol)的实现:classFoo:Baz{varvalue:Sinit(value:S){self.value=value}funcbar(input:T){value=input}}这会产生错误,因为编译器不知道S和T是同一类型。理想情况下,我应该能够编写如下内容:funcbar(input:T){value=input}或funcbar(input:T){v

swift - 我怎样才能使这个 Swift 事件处理程序样板更简洁?

我在Java中看到过一种模式,它允许您以类型安全的方式实现回调列表的子集,并与使用回调的类内联:registerHandlers(newClassWithNoOpMethods(){@overridepublicvoidonFooEvent(FooEventevent){...}@overridepublicvoidonBarEvent(BarEventevent){...}}一切都很好而且类型安全。我想在Swift中做同样的事情,但是谷歌搜索没有找到任何(恕我直言)优雅的解决方案。所以我想到了这个:letregistrar=EventSource.getEventRegistrar(

swift - 我如何在结构中实现这个协议(protocol)

我是Swift的新手,我想为数据库访问创建一个抽象工厂。这是我的协议(protocol)protocolIDAOFactory{associatedtypeDAO:IDAOfunccreateAccountDAO()->DAO}structRealmFactory:IDAOFactory{}protocolIDAO{associatedtypeTfuncsave(object:T)}protocolIAccountDAO:IDAO{}structAccountDAORealm:IAccountDAO{}如何实现structRealmFactory中的IDAOFactory和struct

swift - 在实现类型时针对已弃用的 Swift 协议(protocol)方法发出警告

假设我有一个带有bar()方法的协议(protocol),该方法具有默认实现—本质上是使协议(protocol)要求对于实现类型可选的Swift方式:protocolFoo{funcbar()}extensionFoo{funcbar(){print("defaultbar()implementaion")}}现在假设我决定重命名该方法barrrr(),因为越多r越好:protocolFoo{funcbarrrr()}extensionFoo{funcbarrrr(){print("defaultbarrrr()implementaion")}}现有代码可能仍会使用旧名称实现该方法:c

ios - Swift 中的 Objective-C 协议(protocol)

我正在使用Fusumacocoalpod框架。然而,我发现他们有一个Objective-C协议(protocol),甚至认为整个项目都是用Swift编写的。@objcpublicprotocolFusumaDelegate:class{funcfusumaImageSelected(image:UIImage)optionalfuncfusumaDismissedWithImage(image:UIImage)funcfusumaVideoCompleted(withFileURLfileURL:NSURL)funcfusumaCameraRollUnauthorized()optio

ios - 响应链 - touchesMoved 与 touchesBegan 的不同行为?

假设您创建了两个ViewController,A和B。A有一个segue到B(我认为具体哪个segue在这里并不重要,因为结果似乎是相同的。我将使用push作为示例。).A有以下实现:classA:UIViewController{overridefunctouchesBegan(_touches:Set,withevent:UIEvent?){print("Areceivedabegintouch")}overridefunctouchesMoved(_touches:Set,withevent:UIEvent?){print("Areceivedamovetouch")}}在B中,

ios - Swift 3 面向协议(protocol)编程导致随机 SIGBUS 崩溃

我负责一个完整的Swift3应用程序,经常发生的崩溃之一是一个我根本无法理解的SIGBUS信号:Thread0Crashed:0libswiftCore.dylib0x00000001009b4ac80x1007b8000+20835281LeadingBoards@objcPageView.prepareForReuse()->()(inLeadingBoards)(PageView.swift:0)+11141962LeadingBoardsspecializedReusableContentView.reuseOrInsertView(first:Int,last:Int)->(

swift - 如何使用 if-let 在 Swift 中进行类型删除?

我看到很多关于类型删除的文章。但他们的大多数示例都侧重于将不同类型放入数组中。有什么方法可以让这段代码正常工作吗?protocolA{associatedtypeDatafuncprintThis(value:Data)}classB{}letx=B()iflety=xas?A{//Igeterroronthisline//Donothing}Xcode错误状态协议(protocol)“A”只能用作通用约束,因为它具有Self或关联类型要求此示例代码仅用于演示目的。 最佳答案 从Swift4开始,具有关联类型要求的协议(protoc

swift - 在泛型函数中使用协议(protocol)的关联类型

我正在尝试编写一个简单的MVP模式以在我的应用程序中遵循,因此我编写了两个协议(protocol)来定义ViewController和演示者:protocolPresenterType:class{associatedtypeViewController:ViewControllerTypevarviewController:ViewController?{getset}funcbind(viewController:ViewController?)}protocolViewControllerType:class{associatedtypePresenter:PresenterTy