SwiftUI 的 The compiler is unable to type-check this expression in reasonable time 問題

撰寫 SwiftUI 程式時,有時我們會遇到奇怪的問題,比方像以下的程式,明明語法沒錯,但 Xcode 卻無法理解,顯示錯誤訊息。

The compiler is unable to type-check this expression in reasonable time。
struct ContentView: View {
var body: some View {
List(0..<100) { number in
if number % 3 == 0 || number % 5 == 0 {
Text("number \(number) 是 3 或 5 的倍數")
} else {
Text("number \(number) 不是 3 或 5 的倍數")
}
}
}
}

通常這類問題跟我們在 ViewBuilder 參數傳入的 closure 程式太複雜有關,此時我們可以試著用以下解法修正。

--

--

彼得潘的 iOS App Neverland
彼得潘的 iOS App Neverland

Written by 彼得潘的 iOS App Neverland

彼得潘的 Swift iOS App 程式設計入門,文組生的 Swift iOS App 程式設計入門,彼得潘的 Flutter 跨平台 App 程式設計入門講師,彼得潘的 Swift 程式設計入門,App程式設計入門作者,http://apppeterpan.strikingly.com

Responses (1)