2

my code is like below,

struct RegistrationInformation {
    let leftTitle: String
    let rightTitle: String

}

struct RegisterData{
    let vValueL : String
}
var regDatas : [RegistrationInformation] = []

       for i in 0...3 {
            regDatas.append(RegistrationInformation(leftTitle: regData[i].vValue, rightTitle: ""))
        }
        for i in 4...7 {
            regDatas.append(RegistrationInformation(leftTitle: "", rightTitle: regData[i].vValue))
        }

//output of regDatas is 

       [Datas.RegistrationInformation(leftTitle: "line 1", rightTitle: ""),
        Datas.RegistrationInformation(leftTitle: "line3", rightTitle: ""),
        Datas.RegistrationInformation(leftTitle: "line5", rightTitle: ""),
        Datas.RegistrationInformation(leftTitle: "line7", rightTitle: ""),
        Datas.RegistrationInformation(leftTitle: "", rightTitle: "line2"),
        Datas.RegistrationInformation(leftTitle: "", rightTitle: "line4"),
        Datas.RegistrationInformation(leftTitle: "", rightTitle: "line6"),
        Datas.RegistrationInformation(leftTitle: "", rightTitle: "line8")]
      

what I want is like below in regDatas

  [Datas.RegistrationInformation(leftTitle: "line 1", rightTitle: "line2"),
        Datas.RegistrationInformation(leftTitle: "line3", rightTitle: "line4"),
        Datas.RegistrationInformation(leftTitle: "line5", rightTitle: "line6"),
        Datas.RegistrationInformation(leftTitle: "line7", rightTitle: "line8")]

I want to change data position in array struct of regDatas. how can we do that thank you

3
  • 2
    See: stackoverflow.com/q/40841663/3141234 Iterate over one of those sequences, and you'll have access to pairs of numbers you can use to fill your registration information Commented Jun 11, 2021 at 4:35
  • @Alexander I have used below code from your link ``` for (left, right) in stride(from: 0, to: input.count - 1, by: 2) .lazy .map( { (input[$0], input[$0+1]) } ) { print(left, right) } ``` how can I store left and right to the RegistrationInformation's left title and right title respectively? Commented Jun 11, 2021 at 6:02
  • 1
    Similar to the way you do it now: You have regDatas.append(RegistrationInformation(leftTitle: "line\(left)", rightTitle: "line\(right)")). Your fundamental issue is that you're making twice as many instances as you need, and each of them has one of their two fields "", which is not at all what you want Commented Jun 11, 2021 at 16:15

2 Answers 2

1
struct RegistrationInformation {

    let leftTitle: String
    let rightTitle: String

}


struct RegisterData{

    let vValueL : String

}


var regData: [RegisterData] = [
    RegisterData(vValueL: "line1"),
    RegisterData(vValueL: "line2"),
    RegisterData(vValueL: "line3"),
    RegisterData(vValueL: "line4"),
    RegisterData(vValueL: "line5"),
    RegisterData(vValueL: "line6"),
    RegisterData(vValueL: "line7"),
    RegisterData(vValueL: "line8")
]

func compressData(elements: [RegisterData]) -> [RegistrationInformation]{
    var regDatas : [RegistrationInformation] = []
    for i in 0..<elements.count {
        if i != (elements.count-1){
            regDatas.append(RegistrationInformation(leftTitle: regData[i].vValueL, rightTitle: regData[i+1].vValueL))
        }else{
            regDatas.append(RegistrationInformation(leftTitle: regData[i].vValueL, rightTitle: ""))
        }
    }
    return regDatas
}

print(compressData(elements: regData))
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure what you're trying to do, would something like this help:

let regDatas: [RegistrationInformation] = zip(0...3, 4...7)
    .map { left, right in
        RegistrationInformation(leftTitle: String(left), rightTitle: String(right))
}

or even:

let regDatas: [RegistrationInformation] = (0...3).map { 
    RegistrationInformation(leftTitle: String($0), rightTitle: String($0 + 4))
}

Comments

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.