I am trying to execute the following code and getting the error Could not find member 'subscript
on Xcode6
var b:[[Float]] = [[1,2]]
var i2 = 0 // second to last control point
var pointIm1 = CGPoint(x: 0.0,y: 0.0)
var pointI = CGPoint(x: 0.0,y: 0.0)
var pointIp1 = CGPoint(x: 0.0,y: 0.0)
var px:Float = b[0][0] * Float(pointIm1.x) + b[0][0] * Float(pointI.x) + b[0][0] + b[0][0] * Float(pointIp1.x)
Anyone has idea why is giving error
Edit: Do anyone have better idea or i need to create different variables for each subscript as Nate suggested in his answer
//not looks good
var bj0 = b[j][0]
var bj1 = b[j][1]
var bj2 = b[j][2]
var bj3 = b[j][3]
var px:Float = bj0 * Float(pointIm1.x) + bj1 * Float(pointI.x) + bj2 + bj3 * Float(pointIp1.x)
It isn't a problem with the subscript - Swift gets into trouble when trying to parse longer multi-item expressions like your final line. In a playground I get this error:
You'll need to refactor that line to get past this limitation - something like:
You could probably just break it up into two steps, like this:
It looks like a compiler bug. I tried this expression (obtained from yours, and removing all multiplications):
and it fails, whereas this:
works.
So Nate Cook's answer is correct, it's the compiler getting into trouble.
More tests - using a unidimensional array, this works:
but this doesn't
Note that your expression can be refactored as: