swiftui list a array in swift

Here's an example code on how to Swift UI List an array in Swift:

main.swift
import SwiftUI

struct ContentView: View {
    let fruits = ["Apple", "Banana", "Orange", "Mango", "Watermelon"]
    
    var body: some View {
        List(fruits, id: \.self) { fruit in
            Text(fruit)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
345 chars
18 lines

In the code above, an array of fruits is defined and iterated over in the List view. List is a SwiftUI view that displays a scrollable list of data. In this example, we're using the id parameter to uniquely identify each row in the list. Finally, we add a Text view inside the closure to display each item from the array.

related categories

gistlibby LogSnag