Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

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'


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...