1

I am trying to get the data from my arrays

supposed I have data like this:

[cobaCobaWS.SpecData(lbl1: 1, lbl2: 2, lbl3: 3, lbl4: 4), cobaCobaWS.SpecData(lbl1: 5, lbl2: 6, lbl3: 7, lbl4: 8)]

I'm trying to use for loop to access each array in there but I can't. take a look at my code below:

struct SpecData {
     var lbl1: Int
     var lbl2: Int
     var lbl3: Int
     var lbl4: Int
}

var txtFieldArray = [SpecData]()

for arr in txtFieldArray {
        coreNumber = Double(txtFieldArray[0].lbl1)
        machineNumber = txtFieldArray[0].lbl2
        userNumber = txtFieldArray[0].lbl3
        rdsNumber = txtFieldArray[0].lbl4
    }

The number of arrays will be dynamic because the user can input how many numbers they want. I wish I can put the arr inside like this:

coreNumber = Double(txtFieldArray[arr].lbl1)
machineNumber = txtFieldArray[arr].lbl2
userNumber = txtFieldArray[arr].lbl3
rdsNumber = txtFieldArray[arr].lbl4

I want to put those number into another function afterward. So I want to be able to read every array in my array but I can't i'm thinking maybe because my array from struct? can anyone help me here?

Thankyou in advance

6
  • It's not clear what you want to want to do with the values in the array, the answers you have gotten so far simply overwrites the result for each iteration which looks very wrong. Maybe give an example with say lbl1 which in your sample is first 1 then 5, what do you want to do with 1 and 5? Commented Oct 4, 2020 at 7:49
  • well you got me right. I can continue but then stuck again. So lets say I have that arrays in array like my example. I want to take the value of label 1,2,3,4 from each array and process it to another function and will calculate them for a result. How do I do that? Commented Oct 4, 2020 at 8:23
  • So far here's what i've done: I created variables like coreNumber, machineNumber, userNumber, and rdsNumber. Those will be the temporary variables before getting into the calculating functions. maybe I can make a flow like this: [[array],[array]] --> get value from each array and put into variables --> calculate in a function --> output / result Commented Oct 4, 2020 at 8:27
  • I'm sorry it's a bit long, I tried to explain my best Commented Oct 4, 2020 at 8:28
  • Then you should call the function from inside the loop, for arr in txtFieldArray { someFunc(core: Double(arr.lbl1), machine: arr.lbl2, user: arr.lbl3, rds: arr.lbl4) } Commented Oct 4, 2020 at 8:30

2 Answers 2

4

It's much easier, arr is the current element in each iteration

for arr in txtFieldArray {
    coreNumber = Double(arr.lbl1)
    machineNumber = arr.lbl2
    userNumber = arr.lbl3
    rdsNumber = arr.lbl4
}

Be aware that the ...Number variables will eventually contain only the values of the last item in the array. In practice the loop is pointless.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do it in more Swifty way:

 txtFieldArray.forEach {
      coreNumber.append(Double($0.lbl1))
      machineNumber.append($0.lbl2)
      userNumber.append($0.lbl3)
      rdsNumber.append($0.lbl4)
  }

1 Comment

that's really Swift way, but why this doesn't work for me? it says: Value of type 'Double' has no member 'append'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.