草庐IT

SMB1Protocol

全部标签

swift - 可选协议(protocol)要求,我无法让它工作

我正在处理TheSwiftProgrammingLanguage一书中与可选协议(protocol)要求相关的示例之一。我在以下代码中遇到问题。importFoundation@objcprotocolCounterDataSource{optionalfuncincrementForCount(count:Int)->IntoptionalvarfixedIncrement:Int{get}}@objcclassCounter{varcount=0vardataSource:CounterDataSource?funcincrement(){ifletamount=dataSourc

任何协议(protocol)的 Swift 2.0 协议(protocol)扩展

我试图在swift2.0中的任何协议(protocol)上添加一个方法,但我收到此错误,无法扩展非标称类型“任何”(又名协议(protocol))。知道为什么我无法向Any类型添加协议(protocol)扩展吗?此限制的可能解决方法是什么?我的意图是添加一个名为isPrimitiveType的getter,如果对象是原始对象或实际对象,它会返回true。 最佳答案 从Swift2.1开始,您不能扩展像Any和AnyObject这样的协议(protocol)。也许将来你可以做到这一点。作为解决方法,您可以使用通用的全局自由函数:fun

swift - 协议(protocol)扩展类型自身与初始值设定项类型不匹配

假设出于学术目的,我更喜欢这样的表达方式someInt.asDouble而不是股票Double(someInt)由于所有各种Swift整数类型都符合IntegerType协议(protocol),并且因为似乎有一个Double()初始化程序适合所有这些int类型,我以为我可以这样:extensionIntegerType{varasDouble:Double{returnDouble(self)}}这是行不通的。我想知道为什么?我想知道是否有某种魔法可以让它发挥作用?可能是哪里?或者与self引用有关?我在playground的控制台中遇到的错误是:Playgroundexecutio

swift - 协议(protocol)、willSet 和 didSet

classTriangleAndSquare{vartriangle:EquilateralTriangle{willSet{square.sideLength=newValue.sideLength}}varsquare:Square{willSet{triangle.sideLength=newValue.sideLength}}init(size:Double,name:String){square=Square(sideLength:size,name:name)triangle=EquilateralTriangle(sideLength:size,name:name)}}v

ios - ChatViewController 对协议(protocol)的冗余一致性

我收到这个错误:RedundantconformanceoftheChatViewControllertoprotocol这是我的代码:classChatViewController:ViewController,UITableViewDelegate,UITableViewDataSource我该如何解决? 最佳答案 我相信这意味着您的ViewController类已经实现了UITableViewDelegate或UITableViewDataSource。 关于ios-ChatVie

Swift 使用协议(protocol)扩展默认值

我有protocolErrorContent{vardescriptionLabelText:String{getset}}extensionErrorContent{vardescriptionLabelText:String{return"Hi"}}structLoginErrorContent:ErrorContent{vardescriptionLabelText:Stringinit(error:ApiError){...}}并且xcode提示“没有初始化所有存储的属性就从初始化器返回。”我在这里想要的是只使用我在协议(protocol)扩展中给descriptionLabe

swift - 我们如何在 Swift 中更改协议(protocol)变量

我有两个从同一个协议(protocol)实现的类protocolMyProtocol{}classMyFirstClass:MyProtocol{vartest:Int=0}classMySecondClass:MyProtocol{vartest:Int=0}每次传递MyFirstClass或MySecondClass的对象时,我怎样才能有一个增加测试变量的函数像这样vara=MyFirstClass()varb=MySecondClass()funcinc(myObject:MyProtocol){myObject.test++//myObjecthasnomember`test`

ios - 符合多种协议(protocol)的 Swift 属性

我有符合两种不同协议(protocol)的自定义UIView(CustomView)protocolResizableDelegate:class{funcview(view:UIView,didChangeHeightdifference:CGFloat)}protocolResizable:class{vardelegate:ResizableDelegate?{setget}}protocolTappableDelegate:class{funcviewDidTap(view:UIView)}protocolTappable{vardelegate:TappableDelegat

ios - Swift 类不符合从 swift 协议(protocol)继承的 objective c 协议(protocol)

当它从swift协议(protocol)继承时,我在检查objective-c协议(protocol)一致性时遇到问题。据我了解,以下代码应该打印为真。(swift3)importUIKitprotocolMyProtocol:UITableViewDelegate{}classMyClass:UIViewController,MyProtocol{}letmyClass=MyClass()print(myClass.conforms(to:UITableViewDelegate.self))//printsfalseletviewController=myClassasUIViewC

swift - 是否有与 Swift 的 .description 关联的协议(protocol)?

从Swift2(3?)开始,从任何旧对象获取文本输出的“正确方法”是使用.description。我想在通用函数中使用.description:funccheckNumeric(_value:T)->Bool{letnf=NumberFormatter()nf.numberStyle=.decimalreturn(nf.number(from:value.description)!=nil)}但这不起作用,因为T不支持.description(恕我直言,这是一件非常糟糕的事情)。无论如何,有没有办法做到这一点?有CustomStringConvertible,但没有StringConv