草庐IT

any_variable

全部标签

ios - 错误 : Cannot subscript a value of type '[String : Any]' with an index of type 'UIImagePickerController.InfoKey'

这个问题在这里已经有了答案:Cannotsubscriptavalueoftype'[String:Any]'withanindexoftype'UIImagePickerController.InfoKey'(8个答案)关闭3年前。我正在尝试通过CoreML重建用于图像检测的Apple测试应用程序,但出现错误:Cannotsubscriptavalueoftype'[String:Any]'withanindexoftype'UIImagePickerController.InfoKeyextensionImageClassificationViewController:UIImag

swift - 错误 : Variable with getter/setter cannot have an initial value

如何修复此错误?Variablewithgetter/settercannothaveaninitialvalue这是我的代码:functableView(tableView:UITableView,cellForRowAtIndexPathindexPath:NSIndexPath)->UITableViewCell{varcell:UITableViewCell=tableview.dequeueReusableCellWithIdentifier("cell")asUITableViewCell{//Errorcell.textLabel?.text=self.items[ind

swift - 将 Any 转换为数组

这是我尝试使用的代码structA{varx:Int=0}structB{vary:Int=0}varc:[String:Any]=["a":[A()],"b":[B()]]for(key,value)inc{letarr=valueas![Any]}它只是抛出异常。尝试将Any转换为[Any]时引发运行时异常。我想要实现的主要目标是遍历Any的元素,如果Any是数组的话。对我来说,将Any转换为[Any]是很自然的,但由于某种原因,它不起作用。那么我怎样才能swift做到这一点呢?我看到了一些将Any转换为[A]或[B]的变通方法,但我的情况并非如此,因为数组可以只包含任意结构。

swift - 为什么我们需要显式地将可选类型转换为 Any?

根据Apple文档,TheAnytyperepresentsvaluesofanytype,includingoptionaltypes.SwiftgivesyouawarningifyouuseanoptionalvaluewhereavalueoftypeAnyisexpected.IfyoureallydoneedtouseanoptionalvalueasanAnyvalue,youcanusetheasoperatortoexplicitlycasttheoptionaltoAny,asshownbelow.varthings=[Any]()things.append(3)/

variables - 通过引用访问变量

我正在尝试找出在Swift中引用简单类型的基本方法。在C中,这不是问题:inta=42;int*refA=&a;*refA=43;//Atthispoint,ais43在Swift中,我似乎不能这样做。vara:Int=42println(a)//varaRef:Int=&a//Nope.//varaRef:Int&=&a//Nah.//inoutvar:IntaRef=&a//Nyet//varinout:IntaRef=&a//Non//varaRef:Int*=&a//Whatareyou,stupid,orstubborn?////aRef=43//Ifanyoftheabov

ios - 覆盖 touchesBegan : Error - method does not override any method from superclass

我正在尝试覆盖UITapGestureRecognizer的自定义子类中的touchesBegan。代码如下。我从这里得到它:Howtoacceleratetheidentificationofasingletapoveradoubletap?.这是公认的答案,但我收到一个错误:方法不会覆盖其父类(superclass)中的任何方法。我已经检查过了,这似乎确实是touchesBegan的签名。帮忙?importUIKitclassUIShortTapGestureRecognizer:UITapGestureRecognizer{lettapMaxDelay:Double=0.3ove

ios - 删除 NotificationCenter 的观察者 - "Variable used within its own initial value"

我不明白如何使用block删除通知的观察者。varblock=NotificationCenter.default.addObserver(forName:.notifName,object:obj,queue:OperationQueue.current,using:{notificationinNotificationCenter.default.removeObserver(block)//Dostuff})这会出现编译器错误“变量在其自身的初始值内使用”。我怎样才能删除这个观察者? 最佳答案 编译器提示是因为它“不知道”闭包

ios - swift 3 : How to assign variable of different option types without multiple if let statements?

首先,如果标题含糊不清,我深表歉意。请随意更改它以符合问题描述。我会尽力描述我的问题,但首先这里有一段代码:ifletvc=currentVCas?FirstViewController{vc.doSameMethod()}elseifletvc=currentVCas?SecondViewController{vc.doSameMethod()}elseifletvc=currentVCas?ThirdViewController{vc.doSameMethod()}基本上,我使用iflet语句检查可选的nil,然后解包并赋值。我在所有3个ViewController中都有doSam

swift - 字符串字典 :Any does not conform to protocol 'Decodable'

这个问题在这里已经有了答案:Fatalerror:DictionarydoesnotconformtoDecodablebecauseAnydoesnotconformtoDecodable(2个答案)关闭4年前。我正在尝试实现一个Decodable来解析json请求,但json请求在对象内部有一个字典。这是我的代码:structmyStruct:Decodable{letcontent:[String:Any]}enumCodingKeys:String,CodingKey{casecontent="content"}但是我收到了这个错误:类型“MyClass.myStruct”不符

swift - [Any] 和 contains() 的数组

我正在尝试测试类型为[Any]的数组包含特定类型的值(例如Int)。我知道Swift不知道如何比较Int和任意类型的值,我想这就是自动完成模板中表达的内容:contains(predicate:(protocol)throws->Bool)所以我尝试了这段代码:letintValue:Int=5//forexampleletisContained=myArrayOfAny.contains({elementinreturn((elementas?Int)==intValue)})...并编译(还不能确定它是否有效);但仍然无法做出predicate:(protocol)的正面或反面部分