SwiftUIMacros

1.1

Wouter01/SwiftUI-Macros

What's New

v1.1

2024-02-25T00:13:40Z
  • The internal struct which conforms to EnvironmentKey now has defaultValue marked as static let instead of static var. This fixes some warnings when strict concurrency is enabled.

SwiftUI-Macros

A library of useful macros for SwiftUI.

Swift 5.9 or later is required to use this package.

Installation

Swift Package Manager

Add the dependency:

dependencies: [
    .package(url: "https://github.com/Wouter01/SwiftUI-Macros.git", from: "1.0.0")
]

Add the dependency to your target:

.target(
   name: "MyTarget",
   dependencies: [.product(name: "SwiftUIMacros", package: "SwiftUI-Macros")]
)

Environment

To allow easier creation of new environment keys, two macros are available:

EnvironmentValue

Apply this macro to a variable inside an EnvironmentValues extension to add it to the environment. The assigned value is the default value and is required, unless the type is Optional. The type can be inferred by its value, just like in normal Swift code.

import SwiftUIMacros

extension EnvironmentValues {
    @EnvironmentValue
    var alignment: Alignment = .center
}

// Expands to
extension EnvironmentValues {
    var alignment: Alignment {
      get {
        self[EnvironmentKey_alignment.self]
      }
      set {
        self[EnvironmentKey_alignment.self] = newValue
      }
    }
    
    struct EnvironmentKey_alignment: EnvironmentKey {
       static var defaultValue: Alignment = .center
    }
}

EnvironmentStorage

Apply this to an EnvironmentValues extension to add the EnviromentValue macro to each variable inside the extension.

import SwiftUIMacros

@EnvironmentStorage
extension EnvironmentValues {
    var alignment: Alignment = .center
    
    var secondaryFont: Font?
    
    var gridLines = 0
}

// Expands to
extension EnvironmentValues {
    @EnvironmentValue
    var alignment: Alignment = .center
    
    @EnvironmentValue
    var secondaryFont: Font?
    
    @EnvironmentValue
    var gridLines = 0
}

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Sun May 05 2024 13:51:14 GMT-0900 (Hawaii-Aleutian Daylight Time)