self.view?.presentScene:在解包一个Optional值时意外发现nil(Swift)

时间:2015-08-12 18:13:43

标签: ios swift null crash

我正在尝试切换场景,但我的应用程序崩溃了,这给了我这个错误:

Sub PlotSeparateChartsByMergedFirstRow()
 Dim rUsed As Range, rMerged As Range, rChtData As Range
 Dim rChtDat1 As Range, rChtDat2 As Range
 Dim iColMerge As Long, iColData As Long
 Dim cht1 As Chart, cht2 As Chart
 Dim lastCol As Integer

 Const iChtHeight As Double = 175
 lastCol = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column     
 Set rUsed = ActiveSheet.Range(Cells(1, 2), Cells(7, lastCol))
 iColMerge = 1

 Do
    iColMerge = iColMerge + 1
    If iColMerge > rUsed.Columns.Count Then Exit Do

    If rUsed.Cells(1, iColMerge).mergecells Then
       Set rMerged = rUsed.Cells(1, iColMerge).MergeArea
       Set rChtData = rMerged.Resize(rUsed.Rows.Count)

       ' x values
       Set rChtDat1 = rUsed.Columns(1)
       Set rChtDat2 = rUsed.Columns(1)

       ' y values
       For iColData = 1 To rChtData.Columns.Count - 1 Step 2
          Set rChtDat1 = Union(rChtDat1, rChtData.Columns(iColData))
          Set rChtDat2 = Union(rChtDat2, rChtData.Columns(iColData + 1))
       Next

       ' charts
       ' 2007, 2010
       Set cht1 = ActiveSheet.Shapes.AddChart(xlColumnClustered, rChtData.left,   rChtData.Height, rChtData.Width, iChtHeight).Chart
       ' 2013
       'Set cht1 = ActiveSheet.Shapes.AddChart2(201, xlColumnClustered, rChtData.Left, rChtData.Height, rChtData.Width, iChtHeight).Chart
       With cht1
          .HasTitle = True
          .ChartTitle.Text = "=" & rUsed.Cells(1, iColMerge).Address(, , , True)

          .SetSourceData rChtDat1, xlColumns

       End With

       ' 2007, 2010
       Set cht2 = ActiveSheet.Shapes.AddChart(xlColumnClustered, rChtData.left, rChtData.Height + iChtHeight, _
       rChtData.Width, iChtHeight).Chart
       ' 2013
       'Set cht2 = ActiveSheet.Shapes.AddChart2(201, xlColumnClustered, rChtData.Left, rChtData.Height + iChtHeight, rChtData.Width, iChtHeight).Chart

        With cht2
          .HasTitle = True
          .ChartTitle.Text = "=" & rUsed.Cells(1, iColMerge).Address(, , , True)
          .SetSourceData rChtDat2, xlColumns
       End With

    End If
    iColMerge = iColMerge + rMerged.Columns.Count - 1
 Loop

 End Sub

这是我切换场景的代码:

crash
crash
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

然后在视图中加载:

func switchscenes() {

    if display >= 2 {
        Player.removeFromParent()
        PlayerRight.removeFromParent()
        PlayerLeft.removeFromParent()
        fireHair.removeFromParent()
        fireHairRight.removeFromParent()
        fireHairLeft.removeFromParent()
        imageRightView.removeFromSuperview()
        imageLeftView.removeFromSuperview()
                                    println("crash")
        var gameplayScene:SKScene = GamePlay(size: self.size)
                                    println("crash")
        self.view?.presentScene(gameplayScene)
                                    println("crash")
        display = 0

    }
}

打印失败两次,但不是第三次显​​示

timer5 = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("switchscenes"), userInfo: nil, repeats: true)

是错误的原因。我怎样才能解决这个问题?

编辑:

GamePlay.swift代码:

self.view?.presentScene(gameplayScene) 

0 个答案:

没有答案