Home > database >  How to join strings from array with indexes in SwiftUI
How to join strings from array with indexes in SwiftUI

Time:12-18

I’m making a todo list app with Swift and I’m trying to implement a share function so people can share their todo lists. I want the message to output as 1. Eat 2. Sleep 3. Game 4. Repeat

I can’t seem to get it to repeat as it only outputs 0. Eat

My code:


ForEach(Array(tasks.enumerated()), id: \.element) { i, element in
    ShareLink(item: "https://www.example.com/", subject: Text("My taskfairy list!"), message: Text(String("\(i). \(element)"))) {
        Image(systemName: "square.and.arrow.up") 
    }
}

CodePudding user response:

Add a computed property to your view that converts the array of tasks into a string

var allTasks: String {
    zip(1...tasks.count, tasks).map { "\($0.0). \($0.1)"}.joined(separator: " ")
}

and then use that in your view

ShareLink(item: "https://www.example.com/", 
          subject: Text("My taskfairy list!"), 
          message: Text(allTasks)) {
    Image(systemName: "square.and.arrow.up") 
}
  • Related