草庐IT

optional_argument

全部标签

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 4 "Extra argument in call"Rxswift

我从Swift3更新到4,我正在使用RxSwift,更新后我遇到了一个错误“调用中的额外参数”,因为它现在期望类型元素。我尝试创建(响应,数据)的元组,但仍然给我一个错误,如图所示。publicstaticfuncdeleteCrush(receiver:String,receiver_type:ProviderType)->Observable{/*Deletecrushidentifiedby`id`*/letparameters:Parameters=["receiver":receiver,"receiver_type":receiver_type.rawValue]print

ios - XCode 升级后出错,ios 8.1,spritekit 和 swift,对象构建 'NSData(contentsOfFile:options:error:)'

我有一个可以运行的游戏,我运行、编译并将其上传到iTunesconnect。但是在更新XCode并尝试使用目标ios8.1(不是8.0)编译我的游戏之后。我收到了这个错误。extensionSKNode{classfuncunarchiveFromFile(file:NSString)->SKNode?{letpath=NSBundle.mainBundle().pathForResource(file,ofType:"sks")//ERROR://'dataWithContentsOfFile(_:options:error:)'isunavailable:useobjectcons

ios - WKInterfaceTable 和 setNumberOfRows 崩溃 "unexpectedly found nil while unwrapping an Optional value"

我正在使用WatchKit,我有一个只有一个表的简单界面,但我只用了几行代码就出错了,也许我忘记了一些非常基本的东西。我的界面:表格内的行具有标识符:和自定义类:Controller由这段代码实现:importWatchKitimportFoundationclassActiveListController:WKInterfaceController{@IBOutletweakvartableView:WKInterfaceTable!overridefuncawakeWithContext(context:AnyObject?){super.awakeWithContext(cont

ios - 将 if 语句更改为 guard 会引发此错误。条件绑定(bind)的初始化程序必须具有 Optional 类型,而不是 '(Bool, String)'

我想将以下if语句更改为守卫。这样做会引发以下错误条件绑定(bind)的初始化程序必须具有可选类型,而不是“(Bool,String)”知道我应该怎么做吗?任何帮助将不胜感激。谢谢dispatch_async(backgroundQueue,{let(success,errmsg)=client.connect(timeout:5)print("Connected",success)ifsuccess{let(success,errmsg)=client.send(str:self.jsonString)print("sent",success)ifsuccess{letdata=cl

iOS 架构 : Object with many optionals

这是一个相当具体的情况,所以我会尝试尽可能多地解释细节。我正在制作一个应获取预订列表的应用程序,可以在其中添加新预订或点击现有预订,并在预订详细信息可编辑的情况下获得有关预订的“详细”View,然后保存它的选项。RESTAPI已在C#中完成,并且没有关于什么可以和不能为null的文档(nil,在Swift的情况下)。所以我结束了:structReservation:Codable{varobjectID:String?varobjectName:String?varobjectPrefix:String?varreservationNumber:String?vargrownUPS:I

swift - 核心数据 [NSSet intersectsSet :]: set argument is not an NSSet

我有两个关系表。在尝试保存产品的ShopItem类中:letproductEntity=NSEntityDescription.entityForName("Product",inManagedObjectContext:self.managedObjectContext!)product=Product(entity:productEntity!,insertIntoManagedObjectContext:self.managedObjectContext!)ifletproduct_title:String=jsonObj["product_title"]as?String{pr

swift 泛型 : Constraining Argument Types

来自C++背景(模板),我很难理解为什么以下Swift代码(泛型)无法编译:funcbackwards(array:[T])->[T]{letreversedCollection=array.sort(>)returnreversedCollection}我的理解是T是一个通用类型,我不对其施加任何约束()并声明array类型为Array.然而这会产生以下错误:Ambiguousreferencetomember'sort()'我知道可以使用协议(protocol)对泛型类型施加约束。但是,在这种情况下,我不希望对T有任何限制。.相反,我想限制第一个参数的类型。我已经阅读了Apple关

swift - "CIImage initWithCVPixelBuffer:options:"失败,因为 iOS 10 不支持其像素格式 p422

在ios10中,我想获取视频采集,但是得到一个错误“[CIImageinitWithCVPixelBuffer:options:]failedbecauseitspixelformatp422isnotsupported.”我的代码是这样的:funcpreviewImage(handle:(image:UIImage?)->Void){dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),{()->Voidindispatch_async(dispatch_get_main_queue()

swift - 使用 Swift Optional 类型的奇怪行为

我在练习Swift的Optional类型时,发现了奇怪的(对我来说)行为。代码:funcreturnOptionalIntFromString(_val:String)->Int?{returnInt(val)}funcreturnIntFromString(_val:String)->Int{returnInt(val)??0}letoptionalStr:String?="10"letmappedC1=optionalStr.map(returnIntFromString)letmappedC2=optionalStr.map(returnOptionalIntFromString