0

I need help. I am trying to bring back separate two array items so that I can apply separate formatting to each i.e. a title and notes. Hence, I cannot bring back one single array.

let pageTitle: [String] = runs.map({ $0.title })
let pageContent: [String] = runs.map({ $0.notes! })

for text in pagesTitle {
    let vc = TextViewController(with: text, text2: nil)
    myControllers.append(vc)
}
            
for text2 in pagesContent {
    let vc = TextViewController(with: nil, text2: text2)
    myControllers.append(vc)
}

I have also tried:

for text in pageTitle, pageContent {
    let vc = TextViewController(with: text, text2)
    myControllers.append(vc)
}

And

for text in pageTitle {
    for text2 in pageContent {
        let vc = TextViewController(with: text, text2)
        myControllers.append(vc)
    }
}

The text and text2 are needed for my init:

init(with text: String, text2: String) {
    self.myText = text
    self.myText2 = text2
    myTextView.text = self.myText
    myTextView2.text = self.myText2
    super.init(nibName: nil, bundle: nil)
}

1 Answer 1

1

You can try

let myControllers = runs.map {  TextViewController(with:$0.title, text2:$0.notes!) }

OR

let myControllers = zip(pageTitle,pageContent).map {  TextViewController(with:$0, text2:$1)  }
Sign up to request clarification or add additional context in comments.

10 Comments

Hi @Sh_Khan What would I put in the for loop? your suggestion doesn't work as I need a for loop to generate the required number of VCs.
I do not understand, I thought I needed a for loop to generate the required numbers of VCs?
let myControllers = is what you need in answer
When I remove the for loop and just put in the above, I no longer get any view controller generated?
I believe I need the for loop to generate the number of vcs else how will swift know how many vis to generate? @sh_khan
|

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.