草庐IT

any_option

全部标签

Swift Optionals - 条件中的变量绑定(bind)需要初始化程序

我是Swift的新手,正在尝试弄清楚Optional的概念。我在Playground中有一小段代码给我“条件中的变量绑定(bind)需要初始化程序”错误。有人可以解释为什么以及如何解决它吗?我只想根据“score1”是否有值来打印"is"或“否”。这是代码:importCocoaclassPerson{varscore1:Int?=9funcsum(){ifletscore1{print("yes")}else{print("No")}}//endsum}//endpersonvarobjperson=person()objperson.sum() 最佳答案

ios - 如何在 Codable 类型中使用 Any

我目前在我的项目中使用Codable类型并遇到问题。structPerson:Codable{varid:Any}上述代码中的id可以是String或Int。这就是id类型为Any的原因。我知道Any不是Codable。我需要知道的是如何让它发挥作用。 最佳答案 量子值首先,您可以定义一个可以从String和Int值中解码的类型。在这里。enumQuantumValue:Decodable{caseint(Int),string(String)init(fromdecoder:Decoder)throws{ifletint=try?

ios - 'Any[]' 类型的不可变值在 Swift 中只有名为 'append' 的可变成员,尽管数组定义为 'var'

我想将一个新对象附加到我的数组,它被定义为var在我的Swift应用程序中,尽管我将其定义为var,当我尝试附加它时发生以下错误。`Immutablevalueoftype'Any[]'onlyhasmutatingmembersnamed'append'`这是我的代码:varcontactsArray:Any[]!funcpopoverWillClose(notification:NSNotification){ifpopoverTxtName.stringValue!=""&&popoverTxtContactInfo.stringValue!=""{contactsArray.a

swift - Any 在 Swift 中的转换失败?协议(protocol)

仅供引用:此处出现的Swift错误:https://bugs.swift.org/browse/SR-3871我遇到了一个奇怪的问题,即转换不工作,但控制台将其显示为正确的类型。我有一个公共(public)协议(protocol)publicprotocolMyProtocol{}我在一个模块中实现它,使用一个返回实例的公共(public)方法。internalstructMyStruct:MyProtocol{}publicfuncmake()->MyProtocol{returnMyStruct()}然后,在我的ViewController中,我触发一个将该对象作为发送者的segu

swift - if nil != optional ... 和 if let _ = optional ... 之间有什么区别?

我需要测试返回可选值的表达式是否为nil。这看起来很简单,但这是代码。ifnil!=self?.checklists.itemPassingTest({$0===note.object}){…}出于某种原因,我觉得这看起来很不愉快。ifletitem=self?.checklists.itemPassingTest({$0===note.object}){…}对我来说看起来好多了,但我实际上并不需要该元素,我只需要知道是否有返回。所以,我使用了以下内容。iflet_=self?.checklists.itemPassingTest({$0===note.object}){…}我是不是漏

swift - 使用 Decodable 进行 JSON 解析时,optional 和 decodeIfPresent 有什么区别?

我第一次使用Swift4的Codable协议(protocol),我无法理解Decodable的decodeIfPresent的使用。///Decodesavalueofthegiventypeforthegivenkey,ifpresent.//////Thismethodreturns`nil`ifthecontainerdoesnothaveavalueassociatedwith`key`,orifthevalueisnull.Thedifferencebetweenthesestatescanbedistinguishedwitha`contains(_:)`call.///

swift - 如何在 Swift 2.0 中使用 guard "strongify"optional self

有一个similarquestion关于如何weakify/strongifyself,已回答,但我想知道如何使用“self”而不会因iflet:WelcometoAppleSwiftversion2.0(700.0.59700.0.72).Type:helpforassistance.2>importFoundation3>classFoo{4.funcguardOptSelf()->()throws->Void{5.return{[weakself]in6.guardletself=selfelse{throwNSError(domain:"Iwasdestroyed!",code

swift - NSString boundingRectWithSize :options:attributes:context: not usable in Swift?

我收到错误...找不到接受所提供参数的“init”的重载...当我尝试使用...extensionUIFont{funcsizeOfString(string:String,constrainedToWidthwidth:Double)->CGSize{NSString(string).boundingRectWithSize(CGSize(width,DBL_MAX),options:NSStringDrawingOptions.UsesLineFragmentOrigin,attributes:[NSFontAttributeName:self],context:nil).size

ios - 应用程序(_ :didFinishLaunchingWithOptions:)' nearly matches optional requirement

安装Xcode8beta6后,我收到一条警告:Instancemethod'application(_:didFinishLaunchingWithOptions:)'nearlymatchesoptionalrequirement'application(_:didFinishLaunchingWithOptions:)'ofprotocol'UIApplicationDelegate'在我的AppDelegate中。有2个建议的fixit可以消除警告:将方法标记为私有(private)在方法中添加@nonobjc做任何一个都会消除警告。但为什么需要这样做?

swift - 二元运算符 '===' 不能应用于类型 'Any?' 和 'UIBarButtonItem!' 的操作数

以下代码以前可以在swift2.2编译,现在swift3.0不能编译了。我们如何解决这个问题?Error:Binaryoperator'==='cannotbeappliedtooperandsoftype'Any?'and'UIBarButtonItem!'overridefuncprepare(forsegue:UIStoryboardSegue,sender:Any?){ifsender===saveButton{//Error!//...}elseifsender===closeButton{//Error!//...}} 最佳答案