jjzjj

objective-c - Obj-C 到 Swift : Block to Closure/Delegate

保持ViewController精简并使用MVVMC帮助我更轻松地进行维护。Obj.io关于他们的site有一个很好的教程.不幸的是,该教程仅在Objective-C中。我正在尝试切换到Swift并且移动非常swift,直到我到达用于配置单元的block。在教程中,他们创建了一个blocktypedef:typedefvoid(^TableViewCellConfigureBlock)(idcell,iditem);在创建单元格时返回cellForRowAtIndexPath中的单元格。下面是一些代码,这里是整个项目:Projectvoid(^configureCell)(PhotoC

swift 泛型 : Custom closure with multiple arguments for filter function

我有一个缓存数组,可以存储不同类型的对象,如UIView、UICollectionReuableView等vararrCache=[AnyObject]()我想通过传递自定义闭包来使用内置过滤器函数过滤掉这些特定元素:privatefuncreusableViewsClosure(element:AnyObject,type:T)->Bool{returnelementisT?true:false}现在,当我在过滤器函数上调用这个闭包时,我得到一个错误说明leti=arrCache.filter(reusableViewsClosure(UIView))//错误:无法将调用结果类型bo

swift - Swift 中 Lazy var 和 var as-a-closure 的区别

我创建了一些示例项目来测试各种类型的变量实现,以测试哪些只执行一次,哪些每次调用都执行classSomething:NSObject{varclock:Int=0overridevardescription:String{letdesc=super.descriptionclock+=1return"\(desc)Clock:\(clock)"}}staticvarstaticVar:Something{print("staticVar")returnSomething()}staticvarstaticVar2:Something={print("staticVarII")retur

ios - 将文件转换为 Swift 3 : unable to infer closure type in the current context

我正在转换我的应用程序中的一些库代码,但我不知道如何将该文件从Swift2.3转换为Swift3importUIKitstructConstraint{varidentifier:String?varattribute:NSLayoutAttribute=.centerXvarsecondAttribute:NSLayoutAttribute=.notAnAttributevarconstant:CGFloat=0varmultiplier:CGFloat=1varrelation:NSLayoutRelation=.equal}funcattributes(attrs:NSLayou

Swift Closure 单语句问题

这里我有一个简单的代码片段,可以在我定义的UIView中使用简单的动画。UIView.animateWithDuration(0.1){[weakself]inself?.popOverView.center=gesture.locationInView(self?.view)}这里[weakself]是为了避免引用循环,我也使用尾随闭包来简化代码。然而,编译器对此不满意并给我错误的信息。Cannotinvoke'animateWithDuration'withanargumentlistoftype'(FloatLiteralConvertible,()->()->$T2)'$T2代

ios - UILabel 上的 Swift map "Anonymous closure argument not contained in closure"

为UITableViewCell的实例在contentView上对addSubview()进行三个单独的调用可能会简化为Swiftmap(_:):[nameLabel,numberLabel,informationLabel].map(contentView.addSubview($0))然而,简写会抛出一个错误:“闭包中不包含匿名闭包参数”。.forEach会是这里最好的吗? 最佳答案 此代码无效,因为它使用了一个匿名闭包参数$0,但没有在闭包中。[nameLabel,numberLabel,informationLabel].m

ios - Swift Closure 为什么调用函数会返回错误?

只是学习闭包和嵌套函数。给定下面的嵌套函数:funcprinterFunction()->(Int)->(){varrunningTotal=0funcprintInteger(number:Int){runningTotal+=10println("Therunningtotalis:\(runningTotal)")}returnprintInteger}为什么调用func本身有错误,而我把func赋值给常量却没有错误?printAndReturnIntegerFunc(2)将2Int作为参数传递到哪里以获得返回值?printerFunction(2)//errorletprint

swift - 带有 RxSwift 的 MVVM-C : '[weak self]' in closure

我正在使用RxSwift开发一个iOS项目,我使用带协调器模式的MVVM。这里是我的实现:View模型://MARK:-PrivateprivateletshowNextViewSubject=PublishSubject()//MARK:-InputsvarshowNextView:AnyObserver{returnshowNextViewSubject.asObserver()}//MARK:-OutputsvardidShowNextView:Observable{returnshowNextViewSubject.asObservable()}ViewController:p

ios - 错误 : Assigning non-escaping parameter 'publicationQuery' to an @escaping closure

我有一个像这样的ViewController:classPublicationListViewController:UIViewController{varpublicationQuery:(()->[Publication])!funcinitWith(title:String,publicationQuery:()->[Publication]){self.title=titleself.publicationQuery=publicationQuery}}为什么我会收到“将非转义参数‘publicationQuery’分配给@escaping闭包”错误?

ios - Swift 2.0 'unexpected trailing closure' 惰性变量分配错误

我正在将一个项目转换为Swift2.0,并且在我使用惰性var的所有地方都会遇到这个错误。此代码在1.2中完美运行但在2.0中中断:lazyprivatevarplaceholderImage=UIImage(named:"theImage")但是,此代码会在2.0中生成“意外的尾随闭包”错误。按照Xcode的建议修复错误,这就是我得出的结论:lazyprivatevarplaceholderImage:UIImage=UIImage(named:"theImage")!这可以编译并且似乎可以工作,但我不明白为什么首先需要进行更改。 最佳答案