SwiftUI: type-checkに失敗してコンパイルが通らないことがある問題

p0dee
1 min readJun 2, 2020

以前紹介した、SegmentedControlの再現実装でつまづいたことを思い出したのでメモ。

実装し始めの頃に、無邪気に実装していると、ある時点で以下のようなコンパイルエラーが発生するようになった。

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

その頃の状況を再現すると、以下のような実装であった。(完成形の実装はこちら。)

型推論が原因であるならばと、この中で型が明示されていないForEachクロージャのパラメタindexに着目した。このindexを直接使用しているすべての箇所について、example(index: Int)のように関数に切り出して、関数のパラメタによって型を明示してみると、この問題は解決できた。

上記例では該当箇所はindex == self.selectedIndexのみだが、実はこうした部分がいくつもあったのだった。

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response