【Swift UI】エラー解決まとめ!原因と対策

この記事からわかること
- Swift UIのエラー解決まとめ
index
[open]
\ アプリをリリースしました /
超初心者の私がSwift UIを用いてiOSアプリを開発している時に発生したエラーを備忘録のために、その解決法や原因をまとめていきます。
Swift UIで発生したエラー
この記事で解説、記述しているエラーは以下の通りです。
- Type '()' cannot conform to 'AccessibilityRotorContent'
- Closure containing control flow statement cannot be used with result builder 'ViewBuilder'
- Expressions are not allowed at the top level
Type '()' cannot conform to 'AccessibilityRotorContent'
発生したエラー
Type '()' cannot conform to 'AccessibilityRotorContent'
状況
SwiftUIファイルでForEachを使って配列を表示させようとしていた時に発生
原因のコード
解決したコード
エラーの原因
SwiftUIのViewできるものを表示したいのにprintしていた
Closure containing control flow statement cannot be used with result builder 'ViewBuilder'
発生したエラー
Closure containing control flow statement cannot be used with result builder 'ViewBuilder'
状況
配列に格納されたデータをSwiftUIファイルでforを使って表示させようとした
原因のコード
エラーの原因
SwiftUI内でforではViewを表示できない
Expressions are not allowed at the top level
発生したエラー
Expressions are not allowed at the top level
翻訳:トップレベルでの表現は許可されていません
状況
構造体を定義しているSwiftファイルにDateFormatterの設定を変更しようとした際に発生
原因のコード
解決したコード
エラーの原因
構造体の中に記述すれば解決した。DateFormatterの設定などはトップレベル(プロジェクト.swiftという名称のファイルのみ)で記述しないといけないコードのようです。
まだまだ勉強中ですので間違っている点や至らぬ点がありましたら教えていただけると助かります。
ご覧いただきありがとうございました。