LoginSignup
0
0

More than 1 year has passed since last update.

AppCodeとInjectionIIIを使ってホットリロードを有効にするときにハマったこと

Posted at

前提

AppCode, swift素人
入門書読まず、Generateされたコードを触って初日の人

やりたいこと

AppCodeが公開している方法でInjectionIIIを使ってシミュレータをホットリロードしたい
https://pleiades.io/help/objc/create-a-swiftui-application.html#interactive_preview

結論

struct ContentView_Previewsclass ContentView_Previews に書き換える
スクリーンショット 2022-09-19 19.18.46.png

詳細

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.

エラー2

Other Linker Flags に追記してるけどエラー出て納得行かない。
リンク先見ててもラバーダックで自己解決してた。

⚠️ Injection may have failed. Have you added -Xlinker -interposable to the "Other Linker Flags" of the executable/framework? ⚠️
0
0
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
0