0

This trivial piece of code in a Playground sets the Running wheel spinning forever, and it never prints output. Other attempts to create a 2D array have led to an infinite Indexing loop in my real project. I can't go to Xcode 10 due to my hardware being too old. Has anyone got working code for 2D arrays?

import Cocoa

var str = "Hello, playground"

var arr =  [[Int?]] ()
arr[1][2] = 17

print(str)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Joymaker
  • 813
  • 1
  • 9
  • 23
  • 2
    The code you posted should be causing a `Fatal error: Index out of range` since the arrays contain no elements and your trying to change one that doesn't exist. – Craig Siemens Jul 26 '19 at 23:25
  • yes in another attempt i saw that but this one can't even get that far! – Joymaker Jul 27 '19 at 21:18
  • `var arr: [[Int?]] = Array(repeating: Array(repeating: nil, count: 5), count: 5)` will give you a working 5x5 array. – Cœur Jul 30 '19 at 05:29
  • Possible duplicate of [Two-dimensional array in Swift](https://stackoverflow.com/questions/25127700/two-dimensional-array-in-swift) – Cœur Jul 30 '19 at 05:30

0 Answers0