create a list of strings in swiftui in swift

To create a list of strings in SwiftUI, you can use the List view and provide it with an array of strings. Here's an example:

main.swift
struct ContentView: View {
    let fruits = ["Apple", "Banana", "Orange", "Grape"]
    
    var body: some View {
        List(fruits, id: \.self) { fruit in
            Text(fruit)
        }
    }
}
200 chars
10 lines

In this example, we have an array of fruits which we will use to create the list. We pass the array to the List view, provide it with a unique identifier using id: \.self, and then specify what we want to display for each item in the list using a closure with a Text view.

The result will be a list with each fruit in its own row, like this:

  • Apple
  • Banana
  • Orange
  • Grape

related categories

gistlibby LogSnag