草庐IT

lexical-closures

全部标签

swift - "Missing return in a closure expected to return ' .map 中的 SomeType '"错误

我有以下代码:structAInt{varaInt:Int}structADouble{varaDouble:Doublestaticfuncconvert(aInt:AInt)throws->ADouble{returnADouble(aDouble:Double(aInt.aInt))}}structB{funcdoAction(aInts:[AInt])throws->[ADouble]{returnaInts.map{aIntindo{tryADouble.convert(aInt)}catch{print(error)}}//^^^errorhere:Missingretur

swift 3 : Converting enum case with associated value to closure with protocol parameter results in a compiler error

我有一个枚举,其关联值为结构。当我编写这段代码时,它编译没有错误:protocolMyProtocol{}structMyAssociatedValue:MyProtocol{}enumMyEnum{casemyCase(MyAssociatedValue)}funcmyEnumClosureMapping()->(MyAssociatedValue)->MyEnum{returnMyEnum.myCase}但是我添加了另一个这样的函数:funcmySecondEnumClosureMapping()->(MyProtocol)->MyEnum{returnMyEnum.myCase}

ios - swift/iOS SDK : Generic Function with Class Type/Name Closure/Block Issue

我正在尝试编写一个通用类,它采用自定义类名并创建该类名的实例。在创建之前和之后,我做了一些适用于所有类的通用内容。我想向特定于类的实例添加一些参数。这就是为什么我添加了一个闭包,该闭包在使用实例对象本身作为闭包参数创建实例后被调用。关闭是可选的。有趣的是,在没有闭包的情况下调用函数工作得很好,如果我添加闭包,我会得到以下编译器错误:Expectedmembernameorconstructorcallaftertypename此编译器错误适用于第一个参数,这没有任何意义,因为仅使用第一个参数调用该函数就可以正常工作...我添加了我的简单示例以供Playground使用:classBas

swift - 解释 Swift Closure 语法

我是Swift和闭包的新手,并且正在寻求一些关于到底发生了什么的帮助。示例1:funcgetData(completionHandler:((NSArray?,NSError?)->Void)?)->Void{那么函数getData,有一个completionhandler,其中NSArray+NSError是传递给函数的可选参数?以下位->Void)吗?返回类型是否为void,即没有设置要返回且整个闭包是可选的?然后我不确定下面的->Void是什么意思在这种情况下?示例2:lettask=session.dataTaskWithURL(url!,completionHandler:{

ios - 无法使用 Swift Closure 作为参数调用函数

我编写了一个函数来进行Web服务调用、获取一些JSON、使用数据形成一个数组,并在完成时将其返回到闭包中。我是这种语法的新手,但编译器说它是正确的,所以我假设它是正确的。classAPIHelper:NSObject{funcgetArticles(completion:(result:NSArray,error:NSError)->()){}}我的问题是,我不知道如何调用这个方法。当我尝试时,自动完成不会显示我的完成关闭。相反,它的行为就像我应该将该方法传递给它在(APIHelper)中声明的类的实例。//ViewControlleroverridefuncviewDidLoad()

ios - NSNotification Observer Closure 在 Observer 被移除时没有被移除?

我在ViewController中有以下代码来注册我的自定义通知之一。到目前为止,我一直使用选择器进行注册,但我想我会尝试使用闭包,但发现有些奇怪。NSNotificationCenter.defaultCenter().addObserver(self,selector:"notificationReceived:",name:"NotificationKey",object:nil)NSNotificationCenter.defaultCenter().addObserverForName("NotificationKey",object:nil,queue:nil){[weak

swift 3 : capture strong self in @escaping closure without asynchronous work

有一个带有以下声明的协议(protocol):typealiasSuggestionSourceCallback=([Suggestion])->()protocolSuggestionSource{funcsuggest(_query:SuggestionQuery,callback:@escapingSuggestionSourceCallback)}有两个类实现了这个协议(protocol)。第一类异步获取建议(通过GCD)finalclassFisrtClass:SuggestionSource{privateletqueue=DispatchQueue(label:"my.a

swift - 错误 : cannot convert value of type '() -> ()' to closure result type 'String' using Swift + PromiseKit

我不熟悉Swift中的promise,并使用PromiseKit尝试在Playground上创建一个非常简单的响应并尝试使用它。我有以下代码:importUIKitimportPromiseKitfuncfoo(_error:Bool)->Promise{returnPromise{fulfill,rejectinif(!error){fulfill("foo")}else{reject(Error(domain:"",code:1,userInfo:nil))}}}foo(true).then{response->Stringin{print(response)}}但是我得到以下错误

Swift 递归函数,返回类型 Closure

下面是一个根据调用的返回函数进行加减的基本函数。funccalculateFunc(_inputValue:Int)->(add:(Int)->(Int),sub:(Int)->(Int)){funcaddFunction(_newValue:Int)->(Int){returninputValue+newValue}funcsubFunction(_newValue:Int)->Int{returninputValue-newValue}return(addFunction,subFunction)}calculateFunc(4).add(2)calculateFunc(4).su

ios - 初始化变量 : 'self' captured by a closure before all members were initialized

我在初始化自定义类时遇到问题。当初始化更新属性的类时,我需要在一些数据上设置一个观察者。最初,属性可以为空,这就是我在启动时设置它们的方式。但是,Xcode仍然会抛出“在所有成员初始化之前由闭包捕获的‘self’”错误。这是代码的简化版本。classFoo{init(){self.usersRef=ref.child("users")self.usersRef.observe(DataEventType.value,with:{(snapshot)in//snapshoterrorchecking//usersis[String]self.users=users})}privateva