草庐IT

ios - swift 3 : What's the safest way to unwrap optional values coming from an array?

首先,我初始化变量以保存股票数据varapplePrice:String?vargooglePrice:String?vartwitterPrice:String?varteslaPrice:String?varsamsungPrice:String?varstockPrices=[String]()我从YQL中获取当前股票价格,并将这些值放入一个数组中funcstockFetcher(){Alamofire.request(stockUrl).responseJSON{(responseData)->Voidinif((responseData.result.value)!=nil)

ios - self.view?.presentScene : unexpectedly found nil while unwrapping an Optional value (Swift)

我正在尝试切换场景,但我的应用程序崩溃并出现此错误:crashcrashfatalerror:unexpectedlyfoundnilwhileunwrappinganOptionalvalue(lldb)这是我切换场景的代码:funcswitchscenes(){ifdisplay>=2{Player.removeFromParent()PlayerRight.removeFromParent()PlayerLeft.removeFromParent()fireHair.removeFromParent()fireHairRight.removeFromParent()fireHai

ios - Swift 中的 UIView 子类 : IBOutlet is found nil while unwrapping

我正在尝试通过子类化UIView在Swift中创建自定义View,并且我有一个名为MyViewPanel.xib的View板,其类已分配给我的自定义View。实现如下:importUIKit@IBDesignableclassMyCustomView:UIView{@IBOutletweakvartitle:UILabel!varquestion:Question{didSet{print("didsetquestion,titleis:\(question.title)")}}overrideinit(frame:CGRect){super.init(frame:frame)}req

ios - Swift UITableViewCell detailTextLabel.text 抛出错误 'fatal error: Can' t unwrap Optional.None'

这是生成表格View的Swift代码。我正在尝试设置带有详细信息标签的tableView。我相信问题的产生是因为if(cell==nil){println("1")cell=UITableViewCell(style:.Subtitle,reuseIdentifier:"CellSubtitle")//cell=tableViewCell}永远不会被调用,因此单元格永远不会使用UITableViewCellStyle.Subtitle样式进行初始化。以下是该方法所需的代码:functableView(tableView:UITableView!,cellForRowAtIndexPat

ios - fatal error : unexpectedly found nil while unwrapping an Optional value in UITableViewCell

问了类似的问题here但这并不能为我解决这个问题。我添加了tableView在ViewController.使用它的数据源和委托(delegate)扩展类并为其添加所需的方法。然后我在此表中创建了一个原型(prototype)单元格(不是单独的.xib)并为此创建了一个类TableViewCell并收集了@IBOutlet:@IBOutletweakvartitleOfAccount:UILabel!@IBOutletweakvarlastModified:UILabel!@IBOutletweakvaraccountImage:UIImageView!@IBOutletweakvar

ios - ( swift )PrepareForSegue : fatal error: unexpectedly found nil while unwrapping an Optional value

细节ViewController:@IBOutletvarselectedBundesland:UILabel!表格ViewController:overridefuncprepareForSegue(segue:UIStoryboardSegue!,sender:AnyObject!){if(segue.identifier=="BackToCalculator"){varvc:FirstViewController=segue.destinationViewControllerasFirstViewControllervc.selectedBundesland.text="Test

ios - ( swift )PrepareForSegue : fatal error: unexpectedly found nil while unwrapping an Optional value

细节ViewController:@IBOutletvarselectedBundesland:UILabel!表格ViewController:overridefuncprepareForSegue(segue:UIStoryboardSegue!,sender:AnyObject!){if(segue.identifier=="BackToCalculator"){varvc:FirstViewController=segue.destinationViewControllerasFirstViewControllervc.selectedBundesland.text="Test

swift 2 : ! , ? -"Value of optional type ".. ."not unwrapped"

Valueofoptionaltype"..."notunwrapped;didyoumeantouse'!'or'?'?谁能解释一下这条错误消息的确切含义?我什么时候应该使用“?”什么时候“!”? 最佳答案 obj?.fn()如果对象不为null,则调用fn成员函数,否则不执行任何操作。另一方面,obj!.fn()断言obj不为空,并调用fn。如果对象为null,则会出现异常。所以这是自信的不同:您要么询问,要么简单地声明可空对象的可空属性。 关于swift2:!,?-"Valueof

swift 2 : ! , ? -"Value of optional type ".. ."not unwrapped"

Valueofoptionaltype"..."notunwrapped;didyoumeantouse'!'or'?'?谁能解释一下这条错误消息的确切含义?我什么时候应该使用“?”什么时候“!”? 最佳答案 obj?.fn()如果对象不为null,则调用fn成员函数,否则不执行任何操作。另一方面,obj!.fn()断言obj不为空,并调用fn。如果对象为null,则会出现异常。所以这是自信的不同:您要么询问,要么简单地声明可空对象的可空属性。 关于swift2:!,?-"Valueof

swift - 在 Swift 中使用可选展开进行映射

假设我有以下api:funcpaths()->[String?]{return["test",nil,"Two"]}我在需要[String]的方法中使用了它,因此我不得不使用简单的map函数将其解包。我目前正在做:funccleanPaths()->[String]{returnpaths.map({$0as!String})}在这里,强制转换会导致错误。所以从技术上讲,我需要解开paths数组中的字符串。我在做这件事时遇到了一些麻烦,而且似乎遇到了一些愚蠢的错误。有人可以帮我吗? 最佳答案 compactMap()可以一步完成:l