Thanks to visit codestin.com
Credit goes to github.com

Skip to content

dankinsoid/swift-inject-preview

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftInjectPreview

This repository contains custom Swift macros for use with the InjectionIII tool, allowing you to preview and inject SwiftUI views, UIViews, and UIViewControllers into a running application without needing to restart the app.

Features

  • SwiftUI View Previews: Use the InjectPreview macro to display a SwiftUI view in a running application.
  • UIKit Previews: Previews for UIView and UIViewController objects can be injected similarly, streamlining UIKit-based UI development.
  • AppKit Previews: Previews for NSView and NSViewController objects can be injected similarly, streamlining AppKit-based UI development.
  • Debug Mode Only: These macros are available only in DEBUG builds, ensuring they do not affect production performance.

Usage

SwiftUI

For SwiftUI views, the InjectPreview macro can be used like this:

import SwiftUI

#InjectPreview {
    Text("Hello, World!")
}

This will inject the view into a running application and also show it in the Xcode canvas, enabling real-time updates.

Tip

You can use SwiftUI property wrappers like @State or @Environment inside the InjectPreview macro.

UIKit / AppKit

For UIView/NSView or UIViewController/NSViewController, similar macros are available. Example usage:

#InjectPreview {
    let label = UILabel()
    label.text = "Hello, UIView!"
    return label
}

XCode canvas

To make this preview work with the Xcode canvas, wrap the body of this macro in a PreviewProvider type:

enum Previews: PreviewProvider {
    #InjectPreview {
        let text = NSText()
        text.string = "Hello, World!"
        return text
    }
}

Requirements

  • Swift 5.9+
  • InjectionIII tool (latest version)
  • These macros are limited to DEBUG builds only.

Installation

Add this repository to your project and ensure that the InjectionIII tool is set up and running during development. The @InjectPreview macros can be used immediately in your codebase following the examples above.

Create a Package.swift file.

// swift-tools-version:5.7
import PackageDescription

let package = Package(
  name: "SomeProject",
  dependencies: [
    .package(url: "https://github.com/dankinsoid/swift-inject-preview.git", from: "1.3.0")
  ],
  targets: [
    .target(
      name: "SomeProject", 
      dependencies: [
          .product(name: "SwiftInjectPreview", package: "swift-inject-preview"),
      ]
    )
  ]
)
@_exported import SwiftInjectPreview

Note

This macro does not add InjectionIII to your target. It is recommended to use HotReloading or other similar packages, or add InjectionIII to your project manually.

Author

dankinsoid, [email protected]

License

SwiftInjectPreview is available under the MIT license. See the LICENSE file for more info.

About

InjectPreview macro for InjectionIII

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages