【问题标题】:fatal error Index out of range致命错误 索引超出范围
【发布时间】:2016-06-14 11:20:58
【问题描述】:

我不明白哪里出错了。请帮我解决这个问题。

这是我的代码:

import SpriteKit

class EncounterManager {
    let encounterNames: [String] = ["EncounterName1",
                                    "EncountersName2",
                                    "EncountersName3"]
    var encounters: [SKNode] = []

    var currentEncounterIndex:Int?

    var previousEncounterIndex:Int?


    init() {
        for encounterFileName in encounterNames {
            let encounter = SKNode()
            if let encounterScene = SKScene(fileNamed: encounterFileName) {
                for placeholder in encounterScene.children {

                    if let node = placeholder as? SKNode {

                        switch node.name! {
                        case "N" :
                            let n = N()
                            n.spawn(encounter, position: node.position)

                        case "NN":
                            let nn = NN()
                            nn.spawn(encounter, position: node.position)

                        case "NNN":
                            let nnn  = NNN()
                            nnn.spawn(encounter, position: node.position)

                        case "GGG":
                            let ggg = GGG()
                            ggg.spawn(encounter, position: node.position)

                        case "GG":
                            let gg = GG()
                            gg.spawn(encounter, position: node.position)

                        case "G":
                            let g = G()
                            g.spawn(encounter, position: node.position)

                        case "F":
                            let f = F()
                            ff.spawn(encounter, position: node.position)

                        default:
                            print("Name error: \(node.name)")
                    }
                }
            }
        }
    }
}

    func addToWorld(world:SKNode) {

        for index in (encounters.count).stride(through: 0, by: -1) {

            encounters[index].position = CGPoint(x: -2000, y: index * 1000)
            world.addChild(encounters[index])

       }

   }

}

这是来自控制台的错误:

fatal error: Index out of range

这是错误所在的行:

encounters[index].position = CGPoint(x: -2000, y: index * 1000)

这是行错误:

EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP, subcode=0x0)

我试图在互联网上找到有关此问题的一些信息,但找不到。

【问题讨论】:

  • 提示:count 元素的数组中最后一个元素的索引是多少?

标签: xcode swift


【解决方案1】:

改变这一行:

for index in (encounters.count).stride(through: 0, by: -1)

到这里:

for index in (encounters.count.predecessor()).stride(through: 0, by: -1)

您收到错误是因为使用第一个版本,步幅从 encounters.count 开始,它位于最后一个有效索引的后面。

【讨论】:

  • 或:for index in encounters.indices.reverse() ...
  • @Viktor Simkó 它有助于在第一个文件中修复它,但现在我在另一个文件中有这个错误。行内:`遇到管理器.encounters[0].position = CGPoint(x: 300, y: 30)`
  • @MartinR 它有助于在第一个文件中修复它,但现在我在另一个文件中有这个错误。行内:`遇到管理器.encounters[0].position = CGPoint(x: 300, y: 30)`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多