David Seek David Seek - 2 years ago 81
Swift Question

Swift / transferring objective c code / iterate in for loop / Type XY has no subscript members

I have the following for loop in Objective C code and am trying to transfer it to Swift.

double lastAx[4],lastAy[4],lastAz[4];
for (int i = 0; i < 4; ++i){
lastAx[i] = lastAy[i] = lastAz[i] = 0;

My Code so far:

var lastAx=[Double](count:4, repeatedValue: 0)
var lastAy=[Double](count:4, repeatedValue: 0)
var lastAz=[Double](count:4, repeatedValue: 0)

for i in 0 ..< 4 {
lastAx[i] = lastAy[i] = lastAz[i]

enter image description here

What am I missing? Help is very appreciated.

Avt Avt
Answer Source

Declare lastAx, lastAy, lastAz as arrays with init(count:repeatedValue:) initializer:

var lastAx = [Double](count:4, repeatedValue: 0)
var lastAy = [Double](count:4, repeatedValue: 0)
var lastAz = [Double](count:4, repeatedValue: 0)

Also you will not have to zero them because these initializers set all of the values to zeros. You won't need the loop from the original code, so just delete it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download