プログラミングスクール最大70%OFF! ←クリックして見に行く

【Warning】Swift Compiler Warning:Coercion of implicitly unwrappable value of type ‘String?’ to ‘Any’ does not unwrap optional

【Warning】Swift Compiler Warning:Coercion of implicitly unwrappable value of type ‘String?’ to ‘Any’ does not unwrap optional

エラー解消方法の備忘録_φ(・_・ )

発生したエラー

Coercion of implicitly unwrappable value of type ‘String?’ to ‘Any’ does not unwrap optional

翻訳

暗黙のうちにアンラップ可能な型’String?’の値を’Any’に強制すると、オプションがアンラップされない

原因

printメソッドは引数にAny(非オプショナル型)のインスタンスを要求している為。

解決手順

おすすめされているAnyに強制する。

FixボタンをクリックすればXcodeが勝手に修正してくれます!
Warning文に表記してある通り、素直に3番目の’As Any’にするのがおすすめ!

選択肢と和訳

・Provide a default value to avoid this warning
この警告を回避するためのデフォルト値を提供する

・Force-unwrap the value to avoid this warning
この警告を回避するために値を強制的にアンラップする

・Explicitly cast to ‘Any’ with ‘as Any’ to silence this warning
as Any’を使って明示的に’Any’にキャストすると警告が出なくなります。

参考サイト

https://github-com.translate.goog/apple/swift-evolution/blob/master/proposals/0054-abolish-iuo.md?_x_tr_sl=en&_x_tr_tl=ja&_x_tr_hl=ja&_x_tr_pto=sc
https://teratail.com/questions/204749

Swiftカテゴリの最新記事