2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Xcode SwiftUIでHot Reloadを行う

Posted at

はじめに

SwiftUIで階層が深い箇所のデザイン適応などをしていると
ある程度はプレビューでも出来るのですが実際の表示も確認したくなった時にビルド→画面遷移のコストがバカになりません。

Flutterみたいにホットリロードしたいなと思っていたのですが、InjectionIIIを活用することで実現できました。

導入したXcodeのバージョンは16.2です。2025/02/05時点で動作を確認しています。

環境構築

1. InjectionIIIのReleasesからInjectionIIIアプリをダウンロードして起動します。

スクリーンショット 2025-02-05 20.21.28.png

起動するとメニューバーに表示されます。

スクリーンショット 2025-02-05 20.23.20.png

2. ホットリロードしたいプロジェクトを開きAdd Package Dependencies...からhttps://github.com/krzysztofzablocki/Inject.gitを追加します。

スクリーンショット 2025-02-05 20.10.11.png

3. 忘れずにFrameworksに追加します。

スクリーンショット 2025-02-05 20.25.53.png

4. Other Linker FlagsのDebugに-Xlinker-interposableを追加します。

スクリーンショット 2025-02-05 20.10.46.png

5. Hot reloadしたいViewにコードを追加します。

import Inject // 追加

struct LargeTextListView: View {
    @ObserveInjection var inject // 追加
    var body: some View {
        List(self.dataList) { textData in
            LargeLineTextView(textData.text)
        }
        .enableInjection() // 追加
    }

6. InjectionIIIアプリのOpen Projectから、Xcodeプロジェクトがあるディレクトリを選択します。

スクリーンショット 2025-02-05 20.22.59.png スクリーンショット 2025-02-05 20.33.51.png

7. Xcodeでアプリを実行します。(Command + R)

実行時にうまく出来ていればと以下のようなXcodeのログが表示されます。

💉 InjectionIII connected /path/to/SmallUIs/SmallUIs.xcodeproj
💉 Watching files under the directory /path/to/SmallUIs

8. 5で追加したViewを表示した状態で、コードを変更し保存すると以下のようなログが表示されて、数秒で変更が反映されます!

💉 Compiling /path/to/SmallUIs/SmallUIs/LargeTextListView.swift
💉 Selecting Xcode /Applications/Xcode16.2.app/Contents/Developer
💉 Loading .dylib ...
💉 Interposed 8 function references.
💉 Injected type #1 'SmallUIs.LargeTextListView'
💉 Injected type #2 'SmallUIs.LargeTextListView.TextData'

最後に

以上です。
XcodeだけではなくVSCodeやCursorでの開発でも使えるらしいです。こういうのをうまく活用して効率化していきたいと思います!

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?