草庐IT

swift - 如何记录可选的闭包函数参数?

如何在Swift4中记录函数的可选闭包参数?假设您有一个将可选闭包作为参数的方法。例如,///Anexamplefunction.///Documentationgoeshere.//////-Parameters:///-optionalClosure:Anoptionalclosure.///-aClosureParameter:Thiswillnotbedisplayed.funcexampleMethod(optionalClosure:((_aClosureParameter:Bool)->Void)?){//Dosomething}aClosureParameter不会被记

swift - 多次调用 Swift 闭包是否正确

我正在设计一个SwiftAPI,我在其中定义了一个协议(protocol)EventTransmitter,其中包含一种传输一批Events的方法。处理一批Events可以以包含成功和失败事件的列表结束。因此,此协议(protocol)的任何实现都应使用失败的事件和/或成功的事件来调用完成。publicenumEventResult{///Whentheeventwastransmittedcorrectly.casesuccess([Event])///Whentheeventfailedandshouldbediscardedcasefailure([Event])}publicp

share - 'NSSecureCoding !' is not a subtype of ' NSURL' 在尝试替换闭包参数类型时使用 Swift

我正在使用loadItemForTypeIdentifier:options:completionHandler:NSItemProvider对象上的方法,用于通过iOS8中的共享扩展从Safari中提取url。在Objective-C中,这段代码编译并工作:[itemProviderloadItemForTypeIdentifier:(@"public.url"options:nilcompletionHandler:^(NSURL*url,NSError*error){//Mycode}];然而,在Swift中,我得到了“NSSecureCoding!”isnotasubtypeo

ios - Swift 4 中的可选闭包

这个问题在这里已经有了答案:CompileerrorinSwift4onparameterpassing(1个回答)关闭5年前。我需要一个按需执行的闭包方法。在Swift3中,我曾经这样做过://DeclareclosurevarchecksPerformed:((Void)->Void)?//Declaration//CallwhenneededchecksPerformed?()//OnlyexecuteswhenchecksPerformediscalledchecksPerformed={_in//dostuffhere}在Swift4中,情况不再如此。修复此警告后,一切都没有

swift - Swift 中闭包的强引用循环

我正在浏览文档(关于“自动引用计数”的章节“闭包的强引用循环”部分),但我似乎无法弄清楚在定义类时我应该保留强引用的情况self(该类的实例)在属性的闭包中。捕获列表似乎始终是避免内存泄漏的最佳解决方案,而且我真的想不出任何应该保持强引用循环的场景。以下是文档给出的示例:classHTMLElement{letname:Stringlettext:String?//WithoutCaptureList@lazyvarasHTML:()->String={iflettext=self.text{return"\(text)"}else{return""}}init(name:String

swift - 如何在 Swift 中创建通用闭包

我想在swift中创建一个使用通用闭包(block)的函数。我的第一次尝试是:funcsaveWithCompletionObject(obj:AnyObject,success:AnyObject->Void,failure:Void->Void)但是一旦我用另一个block调用这个函数,例如:funcdoSomething(success:String->Void,failure:Void->Void){saveWithCompletionObject("Example",success,failure)}我得到一个错误:'AnyObject'不是'String'的子类型提前致谢!

swift - 字典中的闭包

已经发布了一个问题并提供了一个可接受的答案,但截至今天它还没有用。它在PlaygroundExpected','separator中抛出错误AddingSwiftClosuresasvaluestoSwiftdictionaryclassCommandResolver{privatevarcommandDict:[String:()->Void]init(){commandDict=[String:()->Void]()setUpCommandDict()}funcsetUpCommandDict(){commandDict["OpenAssessment_1"]={println("

objective-c - 将 objective-c block 转换为 Swift 闭包

我正在尝试将这个Objective-Cblock转换为Swift:[self.clientdownloadEntity:@"Students"withParams:nilsuccess:^(idresponse){//executecode}failure:^(NSError*error){//Executecode}];这是我在Swift中的代码,但语法似乎有点不对:client.downloadEntity("Students",withParams:nil,success:{(students:[AnyObject]!)->Voidinprint("here")},failure

swift - 理解没有返回类型的闭包语法

我在这里是第一次接触swift,我偶然发现了一个闭包语句,根据我目前对闭包的编写方式的理解,它对我来说没有多大意义。这实际上是一个由两部分组成的问题,因为我也不太了解这个闭包的条件绑定(bind)背后的意图/解释。我抛出的代码块是这样的:FIRAuth.auth()?.createUser(withEmail:email,password:password){(user,error)inifleterror=error{print(error.localizedDescription)return}}我的理解是,闭包需要根据文档定义指定一个返回类型(something1,somethi

swift - 你能为闭包参数列表中的元组元素创建名称吗?

这是一个属性,它为由UInt8组成的颜色提供足够好的亮度表示,用于red、green、和蓝色。varluma:UInt8{returnUInt8([(red,54),(green,183),(blue,19)].reduce(0){let(color,eightBitWeight)=$1return$0+Int(color)*eightBitWeight}>>8)}我希望它看起来像这样,但它无法编译:[(red,54),(green,183),(blue,19)].reduce(0){luma,(color,eightBitWeight)inluma+Int(color)*eightB