0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

String.split(separator:) で Xcode Previews がコケる

Last updated at Posted at 2020-08-05

SwiftUIで画面を実装中していたらXcode Previewsで表示していたプレビューが突然できなくなった。

Diagnosticsを開くとこんなエラー

Compiling failed: global function '__designTimeString(_:fallback:)' requires that 'String.Element' (aka 'Character') conform to 'ExpressibleByStringLiteral'

エラーの行も表示されるので当該箇所を見たら大体こんな感じ

let str = ~
let array = str.split(separator: "\n") // ← ここでエラー発生
    .map(String.init)

どうやら"\n"(String Literal)がXcode Previews上だとCharacterとして認識されないらしい(なぜ?)

解決策

Characterであることを明示すれば良い。

let array = str.split(separator: "\n" as Character)
// or
let array = str.split(separator: Character("\n"))

参考

2020/09/21追記

Xcode12(Swift5.3)でも直っていない模様

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?