Could not find member index

I am trying to execute the following code and get 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)

      

Does anyone have an idea why the error is occurring

Edit: Does anyone have a better view or do I need to create different variables for each index 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)

      

+3


source to share


2 answers


It's not an index issue - Swift gets into a problem when trying to parse longer expressions of multiple elements, like your last line. On the playground I am getting this error:

the expression was too complex to be resolved within a reasonable time frame; consider breaking the expression into separate subexpressions

You will need to refactor this line to overcome this limitation - something like:

let b00 = b[0][0]
var px: Float = b00 * Float(pointIm1.x) + b00 * Float(pointI.x) + b00 + b00 * Float(pointIp1.x)

      




You could just break it down into two steps, for example:

var px:Float = bj0 * Float(pointIm1.x) + bj1 * Float(pointI.x)
px += bj2 + bj3 * Float(pointIp1.x)

      

+2


source


It looks like a compiler error. I tried this expression (derived from yours and removing all multiplications):

var px:Float = b[0][0] + b[0][0]  + b[0][0] + b[0][0] 

      

and it doesn't work, whereas this:

var px:Float = b[0][0] + b[0][0] + b[0][0] 

      

work.

So Nate Cook's answer is correct, the compiler is in trouble.



Additional tests - using a one dimensional array, this works:

var c:[Float] = [1.0, 2.0]
var px2: Float = c[0] + c[0] + c[0] + c[0] + c[0]

      

But it is not

var px2: Float = c[0] + c[0] + c[0] + c[0] + c[0] + c[0] // Cannot invoke '+' with arguments of type '($T28, $T32)'

      

Note that your expression can be refactored as:

var px:Float = b[0][0] * (Float(pointIm1.x) + Float(pointI.x) + 1.0 + Float(pointIp1.x))

      

+1


source







All Articles