'SwiftUI picker on macOS

I try for my personal knowledge to learn SwiftUI. Then I test on my Mac, but my first feeling is that SwiftUI is much more iOS oriented than macOS. Am I right ?

Now the technical question I have an array :

var years = ["1900", "1901", "1902", "1903", "1904", "1905", "1906", "1907", "1910",
             "1911", "1912", "1913", "1914", "1915", "1916", "1917", "1918", "1919", "1920",
             "1921", "1922", "1923", "1924", "1925", "1926", "1927", "1928", "1929", "1930",
             "1931", "1932", "1933", "1934", "1935", "1936", "1937", "1938", "1939", "1940",
             "1941", "1942", "1943", "1944", "1945", "1946", "1947", "1948", "1949", "1950"]

And I want to put these values in a SwiftUI picker.

It seem to be impossible to make a loop to put these value in a Picker

Picker (selection: $toDate, label: Text ("From Date : "), content: {
    for y in years {
                    
    }
})

always give an error:

Closure containing control flow statement cannot be used with function builder 'ViewBuilder'



Solution 1:[1]

Check this:

var years = ["1900", "1901", "1902", "1903", "1904", "1905", "1906", "1907", "1910",
             "1911", "1912", "1913", "1914", "1915", "1916", "1917", "1918", "1919", "1920",
             "1921", "1922", "1923", "1924", "1925", "1926", "1927", "1928", "1929", "1930",
             "1931", "1932", "1933", "1934", "1935", "1936", "1937", "1938", "1939", "1940",
             "1941", "1942", "1943", "1944", "1945", "1946", "1947", "1948", "1949", "1950"]

@State var toDate =  "1900"

var body: some View {
    Picker("From date:", selection: $toDate) {
        ForEach(years, id:\.self ) { i in
            Text(i)
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pkamb