{"id":1066,"date":"2014-11-30T20:45:18","date_gmt":"2014-12-01T02:45:18","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=1066"},"modified":"2018-12-24T08:42:16","modified_gmt":"2018-12-24T14:42:16","slug":"multidimensional-arrays-in-swift","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/multidimensional-arrays-in-swift\/","title":{"rendered":"Multidimensional Arrays in Swift"},"content":{"rendered":"<p>Updated on <b>12\/24\/2018<\/b> for <img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/Swift-4.2-orange.svg?style=flat\" alt=\"Swift 4.2\"\/>.<\/p>\n<h3>Swift 4.2<\/h3>\n<p>The format for defining a 3-dimensional array in Swift 4.2 has changed!<\/p>\n<pre class=\"theme:terminal lang:swift decode:true nums:false\">\nlet a:[[[Int]]] = [[[Int]]](repeating:[[Int]](repeating:[Int](repeating:1, count:3), count:3), count:3)\n<\/pre>\n<p>Enumerating still works as follows:<\/p>\n<pre class=\"theme:terminal lang:swift decode:true nums:false\">\nfor (i,plane) in a.enumerated() {\n  for (j,row) in plane.enumerated() {\n    for (k,cell) in row.enumerated() {\n      print(\"m[\\(i),\\(j),\\(k)] = \\(cell)\")\n    }\n  }\n}\n<\/pre>\n<p>Updated on <b>5\/12\/2018<\/b> for <img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/Swift-4.0-orange.svg?style=flat\" alt=\"Swift 4.0\"\/>.<\/p>\n<h3>Swift 4.0<\/h3>\n<p>The format for defining a 3-dimensional array in Swift 4.0 has not changed, and can still be accomplished with a variation of:<\/p>\n<pre class=\"theme:terminal lang:swift decode:true nums:false\">\nlet a:[[[Int]]] = [[[Int]]](count:3, repeatedValue:[[Int]](count:3, repeatedValue:[Int](count:3, repeatedValue:1)))\n<\/pre>\n<p>What has changed is the <code>enumerate()<\/code> method has been renamed to <code>enumerated()<\/code>:<\/p>\n<pre class=\"theme:terminal lang:swift decode:true nums:false\">\nfor (i,plane) in a.enumerated() {\n  for (j,row) in plane.enumerated() {\n    for (k,cell) in row.enumerated() {\n      print(\"m[\\(i),\\(j),\\(k)] = \\(cell)\")\n    }\n  }\n}\n<\/pre>\n<p>Updated on <b>1\/24\/2016<\/b> to support <img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/Swift-2.2-orange.svg?style=flat\" alt=\"Swift 2.2\" \/>, including removing C-style <code>for<\/code> loops and postfix increment operators.<\/p>\n<p><b>Editor&#8217;s note:<\/b>  If you&#8217;re looking to branch out with Swift on a Linux system, check out our latest series of articles <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/swift-on-linux\/\">Swift on Linux<\/a> and <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/more-swift-on-linux\/\">More Swift on Linux<\/a>.<\/p>\n<p>File this in the &#8220;Well I&#8217;ll be damned&#8221; category.  While working on another project (I&#8217;m interested in a hybrid OS X and iOS project for generating <a href=\"http:\/\/en.wikipedia.org\/wiki\/Mandelbrot_set\">Mandelbrot<\/a> images) I came across the need to store Mandelbrot &#8220;escape values&#8221; in a two-dimensional array (e.g., a matrix).  This matrix will be the basis for drawing the Mandelbrot plot, which each cell value being mapped to a color.<\/p>\n<p>Of course I&#8217;m writing this code in Swift so I looked up how to declare a multidimensional array in the <a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/Swift\/Reference\/Swift_Array_Structure\/index.html\">Swift Standard Library Reference<\/a>.<\/p>\n<p>Let&#8217;s take a look at the syntax for a unidimensional array, and yes, we&#8217;re using the &#8220;sugar&#8221; version of the syntax:<\/p>\n<pre class=\"theme:terminal lang:swift decode:true nums:false\" >class MyClass {\n  var anArray:[Int]\n\n  init() {\n    self.anArray = [3,4,5]\n  }\n\n  func dump() {\n    print(anArray)\n  }\n\n}<\/pre>\n<p>Simple enough.  If we wanted to prefill our array with ones, we can do that with the <code>(count:c, repeatedValue:v)<\/code> construct:<\/p>\n<pre class=\"theme:terminal lang:swift decode:true nums:false\">\nself.anArray = [Int](count:10, repeatedValue:1)\n<\/pre>\n<p>The syntax may look out of place but it&#8217;s not really:  <code>[Int]<\/code> <i>is<\/i> the name of the object we want to construct (an array of <code>Int<\/code>s) and we&#8217;re providing constructor arguments.  It would be no different than writing <code>self.anArray = IntegerArray(count:10, repeatedValue:1)<\/code> if <code>IntegerArray<\/code> provided us with an array of integers.<\/p>\n<p>Now enter the multidimensional array syntax.  For an array of arrays of integers we would write:<\/p>\n<pre class=\"theme:terminal lang:swift decode:true nums:false\">\nvar anArray:[[Int]]\n<\/pre>\n<p>The Swift reference also provides for basic initialization.  Here we create a 3&#215;3 identity matrix:<\/p>\n<pre class=\"theme:terminal lang:swift decode:true nums:false\">\nself.anArray = [[1,0,0], [0,1,0], [0,0,1]]\n<\/pre>\n<p>We&#8217;ll use this again in a basic class:<\/p>\n<pre class=\"theme:terminal lang:swift decode:true nums:false\">\nclass MyClass {\n  \n  var anArray:[[Int]]\n  \n  init() {\n    self.anArray = [[1,0,0], [0,1,0], [0,0,1]]\n  }\n  \n  func dump() {\n    for (i,row) in anArray.enumerate() {\n      for (j,cell) in row.enumerate() {\n        print(\"m[\\(i),\\(j)] = \\(cell)\")\n      }\n    }\n  }\n  \n}\n\nlet m = MyClass()\nm.dump()\n<\/pre>\n<p>Executing this in the Playground gives us<\/p>\n<pre>\nm[0,0] = 1\nm[0,1] = 0\nm[0,2] = 0\nm[1,0] = 0\nm[1,1] = 1\nm[1,2] = 0\nm[2,0] = 0\nm[2,1] = 0\nm[2,2] = 1\n<\/pre>\n<p>which is what we would expect.<\/p>\n<p>Now, I want to store information in a larger array (say 175 by 100), and want to initialize each element to zero.  Here is the syntax:<\/p>\n<pre class=\"theme:terminal lang:swift decode:true nums:false\">\nself.anArray = [[Int]](count:175, repeatedValue:[Int](count:100, repeatedValue:0))\n<\/pre>\n<p>Take a step back and let it soak in and it makes perfect sense.  A two dimensional array is an array of arrays, so it stands to reason that the <code>repeatedValue<\/code> of the outer array will itself be an array.  The <code>repeatedValue<\/code> of the inner array will be the base type you are storing in each cell.<\/p>\n<p>Finally, a 3x3x3 array with each cell initialized to 1.<\/p>\n<pre class=\"theme:terminal lang:swift decode:true nums:false\">\nlet a:[[[Int]]] = [[[Int]]](count:3, repeatedValue:[[Int]](count:3, repeatedValue:[Int](count:3, repeatedValue:1)))\n<\/pre>\n<h2>Swift 3.0<\/h2>\n<p>As mentioned in our <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/moving-to-swift-3-0\/\">Moving to Swift 3.0<\/a> post, the array initializer signature has changed from <code>(count:repeatedValued:)<\/code> to <code>(repeating:count:)<\/code>.  No sweat, let&#8217;s update our 3x3x3 array declaration:<\/p>\n<pre class=\"theme:terminal lang:swift decode:true nums:false\">\nlet a = [[[Int]]](repeating:[[Int]](repeating:[Int](repeating:1,count:3), count:3), count:3)\n<\/pre>\n<p>Cake.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Updated on 12\/24\/2018 for . Swift 4.2 The format for defining a 3-dimensional array in Swift 4.2 has changed! let a:[[[Int]]] = [[[Int]]](repeating:[[Int]](repeating:[Int](repeating:1, count:3), count:3), count:3) Enumerating still works as follows: for (i,plane) in a.enumerated() { for (j,row) in plane.enumerated() { for (k,cell) in row.enumerated() { print(&#8220;m[\\(i),\\(j),\\(k)] = \\(cell)&#8221;) } } } Updated on 5\/12\/2018 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3108,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1066","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/1066"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=1066"}],"version-history":[{"count":17,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/1066\/revisions"}],"predecessor-version":[{"id":2608,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/1066\/revisions\/2608"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media\/3108"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=1066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=1066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=1066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}