简体   繁体   English

“线程 1:致命错误:索引超出范围”错误无法解决

[英]"Thread 1: Fatal error: Index out of range" error can't be resolved

I am following the swiftris tutorial here: https://www.bloc.io/tutorials/swiftris-build-your-first-ios-game-with-swift#!/chapters/679我正在关注 swiftris 教程: https ://www.bloc.io/tutorials/swiftris-build-your-first-ios-game-with-swift#!/chapters/679

This tutorial was built for xcode 7, but i am using xcode 10. I have an error, Thread 1: Fatal error: Index out of range, that I cant seem to resolve.本教程是为 xcode 7 构建的,但我使用的是 xcode 10。我有一个错误,线程 1:致命错误:索引超出范围,我似乎无法解决。 Here is my code:这是我的代码:

class Array2D<T> {
      let columns: Int
      let rows: Int
 // #2
      var array: Array<T?>

      init(columns: Int, rows: Int) {
          self.columns = columns
          self.rows = rows
 // #3
        array = Array<T?>(unsafeUninitializedCapacity:rows * columns, initializingWith: {_,_ in })
      }

 // #4
      subscript(column: Int, row: Int) -> T? {
        get {
            return array[(row * columns) + column]
        }
          set(newValue) {
              array[(row * columns) + column] = newValue
          }
      }
  }

Thank you in advance!先感谢您!

Try changing the line at #3 as shown below:尝试更改 #3 处的行,如下所示:

     init(columns: Int, rows: Int) {
         self.columns = columns
         self.rows = rows
// #3
         array = Array<T?>(repeating: nil, count: rows * columns)
     }

init(unsafeUninitializedCapacity:initializingWith:) is not for usual use cases. init(unsafeUninitializedCapacity:initializingWith:)不适用于通常的用例。


Generally Xcode 7 (Swift 2) is too....o old in the Swift world and you should better find an up-to-date tutorial.通常 Xcode 7 (Swift 2) 在 Swift 世界中太......太老了,你最好找到一个最新的教程。

In the later versions of Swift there is no need to do this as you can create a 2D array directly through 'an array or arrays' like let matrix = [[Int]].在 Swift 的更高版本中,无需执行此操作,因为您可以直接通过“一个或多个数组”创建一个 2D 数组,例如 let matrix = [[Int]]。

However it is still a good exercise to code your own to understand how arrays and indexing work.然而,编写自己的代码以了解数组和索引的工作方式仍然是一个很好的练习。

There are two things that could/should be changed from the example.从示例中可以/应该更改两件事。 Initialises the underlying array with .init(repeating: count:) and change the subscripting indexing as it currently doesn't work.使用.init(repeating: count:)初始化底层数组并更改下标索引,因为它当前不起作用。

For a 0-indexed array with x rows and y columns, ie one ranging from (0,0) to (x-1, y-1)对于具有 x 行和 y 列的 0 索引数组,即范围从 (0,0) 到 (x-1, y-1)

class Array2D<T> {
   let columns: Int
   let rows: Int
   var array: Array<T?>

   init(columns: Int, rows: Int) {
      self.columns = columns
      self.rows = rows
      array = Array<T?>(repeating: nil, count: rows * columns)
   }

   subscript(column: Int, row: Int) -> T? {
      guard row <= rows && column <= columns else {fatalError("invalid position"))
      get {
         return array[(column * columns) + row]
      }
      set(newValue) {
         array[(column * columns) + row] = newValue
      }
   }
}

For a 1-indexed array, ie (1,1) through to (x,y) you'd need to change the subscripting to:对于 1 索引数组,即 (1,1) 到 (x,y),您需要将下标更改为:

   subscript(column: Int, row: Int) -> T? {
      guard row < rows && column < columns else {fatalError("invalid position"))

      get {
         return array[((column - 1) * columns) + (row - 1)]
      }
      set(newValue) {
         array[((column - 1) * columns) + (row - 1)] = newValue
      }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM