I was trying to make a simple 2D array of this struct:
struct Color {
    var r : Float = 0
    var g : Float = 0
    var b : Float = 0
}

As I was reading up on arrays in Swift, I came across this example
initialization:
var threeDoubles = [Double](count: 3, repeatedValue: 0.0)

And there was some mention of immutable arrays being an existing compiler
optimization, so I figured I'd set up my array of arrays all at once so it
could be fast.
After some messing around and guesswork because the Swift autocomplete is
ridiculously slow (I really tried not to complain about Swift for this
entire email, but it's hard), I found that you can nest these calls like so:
var arr1 = [Array](count:6, repeatedValue:[Int](count:8,
repeatedValue:Int()))

This gives you an 8x8 array of arrays of 0s.  Pretty simple and neat and
easy to understand (if a little hard to parse visually).

If I do this, however, it breaks:
var arr2 = [Array](count:6, repeatedValue:[Color](count:8, repeatedValue:
Color()))

Well, to be more accurate, it makes a magical array which contains arrays
of 8 colors at indices 0 and 3, and cthulhu at indices 1, 2, 4, and 5.  If
you access these during the execution of your program, you get:
EXC_BAD_ACCESS (code=EXC_I386_GPFLT).

If you run all these lines of code in a Swift playground (I named mine
Devil's.playground), it won't even generate an inspectable value for this,
instead it shows the same error (no doubt because it was trying to look at
the cthulhu rows).

LLDB has a little bit easier time and prints this:
(lldb) po arr
6 values
 {
  [0] = 8 values {
    [0] = (r = 0, g = 0, b = 0)
    [1] = (r = 0, g = 0, b = 0)
    [2] = (r = 0, g = 0, b = 0)
    [3] = (r = 0, g = 0, b = 0)
    [4] = (r = 0, g = 0, b = 0)
    [5] = (r = 0, g = 0, b = 0)
    [6] = (r = 0, g = 0, b = 0)
    [7] = (r = 0, g = 0, b = 0)
  }
  [1] = {}
  [2] = {}
  [3] = 8 values {
    [0] = (r = 0, g = 0, b = 0)
    [1] = (r = 0, g = 0, b = 0)
    [2] = (r = 0, g = 0, b = 0)
    [3] = (r = 0, g = 0, b = 0)
    [4] = (r = 0, g = 0, b = 0)
    [5] = (r = 0, g = 0, b = 0)
    [6] = (r = 0, g = 0, b = 0)
    [7] = (r = 0, g = 0, b = 0)
  }
  [4] = {}
  [5] = {}
}

It just punts on the rows that cause the problem.

What is the mystery of this 15 line playground:
// Playground - noun: a place where people can play

import Cocoa

var str = "Hello, playground"

struct Color {
    var r : Float = 0
    var g : Float = 0
    var b : Float = 0
}

var arr1 = [Array](count:6, repeatedValue:[Int](count:8,
repeatedValue:Int()))

var arr2 = [Array](count:6, repeatedValue:[Color](count:8,
repeatedValue:Color()))

Any insight would be appreciated.

--
Daniel Blakemore
Pixio Software
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to