前提
AppCode, swift素人
入門書読まず、Generateされたコードを触って初日の人
やりたいこと
AppCodeが公開している方法でInjectionIIIを使ってシミュレータをホットリロードしたい
https://pleiades.io/help/objc/create-a-swiftui-application.html#interactive_preview
結論
struct ContentView_Previews
を class ContentView_Previews
に書き換える
詳細
Before
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
After
class ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
#if DEBUG
@objc class func injected() {
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
windowScene?.windows.first?.rootViewController =
UIHostingController(rootView: ContentView_Previews.previews)
}
#endif
}
#if DEBUG
の範囲内だけ追記するかと脳死して、IDEに怒られて@objc
消したりして実行してた。
公式ドキュメントが古いと思い込んでいた。
エラー出て調べたこと
エラー1
読み込んでもよくわからなかった。
Xcode起動してホットリロードできるか試してたら、エラー2に変わった
Could not locate compile command for /Users/xxx/ContentView.swift.
- https://github.com/johnno1962/InjectionIII/issues/396
- https://github.com/johnno1962/InjectionIII/issues/401
エラー2
Other Linker Flags
に追記してるけどエラー出て納得行かない。
リンク先見ててもラバーダックで自己解決してた。
⚠️ Injection may have failed. Have you added -Xlinker -interposable to the "Other Linker Flags" of the executable/framework? ⚠️