Skip to content

LZhenHong/StorageMacro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Storage

A Swift Macro that automatically applies @AppStorage to properties, simplifying UserDefaults persistence in SwiftUI.

Swift 5.9+ Platforms License

Features

  • Automatically generates @AppStorage attributes for stored properties
  • Supports optional types without explicit initializers (var name: String?)
  • Customizable key prefix and UserDefaults suite name
  • Supports both struct and class types
  • Opt-out mechanism with @nonstorage
  • Skips computed properties, private/fileprivate properties automatically

Requirements

  • Swift 5.9+
  • iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+

Installation

Swift Package Manager

Add the following to your Package.swift:

dependencies: [
  .package(url: "https://github.com/LZhenHong/StorageMacro.git", from: "0.0.3")
]

Or in Xcode: File > Add Package Dependencies... and enter the repository URL.

Usage

Basic Usage

import Storage

@storage
struct Settings {
  var isDarkMode = false
  var fontSize = 14
  var username = "Guest"
}

This expands to:

struct Settings {
  @AppStorage("io.lzhlovesjyq.settings.isdarkmode", store: (UserDefaults(suiteName: "io.lzhlovesjyq.userdefaults") ?? .standard))
  var isDarkMode = false

  @AppStorage("io.lzhlovesjyq.settings.fontsize", store: (UserDefaults(suiteName: "io.lzhlovesjyq.userdefaults") ?? .standard))
  var fontSize = 14

  @AppStorage("io.lzhlovesjyq.settings.username", store: (UserDefaults(suiteName: "io.lzhlovesjyq.userdefaults") ?? .standard))
  var username = "Guest"
}

Custom Prefix and Suite Name

@storage(prefix: "com.myapp", suiteName: "com.myapp.defaults")
struct Settings {
  var isDarkMode = false
}

Excluding Properties

Use @nonstorage to exclude specific properties:

@storage
struct Settings {
  var persistedValue = true

  @nonstorage
  var temporaryValue = false  // Won't have @AppStorage applied
}

Automatically Skipped Properties

The macro automatically skips:

  • let constants
  • private or fileprivate properties
  • Computed properties
  • Properties without default values (except optional types)
  • Properties already marked with @AppStorage
@storage
struct Settings {
  var stored = true           // ✅ Gets @AppStorage
  var name: String?           // ✅ Gets @AppStorage (optional type)
  let constant = "value"      // ⏭️ Skipped (constant)
  private var secret = ""     // ⏭️ Skipped (private)
  var computed: Int { 42 }    // ⏭️ Skipped (computed)
  var noDefault: String       // ⏭️ Skipped (no default value, non-optional)
}

API Reference

@storage

Parameter Type Default Description
prefix String "io.lzhlovesjyq" Key prefix for AppStorage
suiteName String "io.lzhlovesjyq.userdefaults" UserDefaults suite name

@nonstorage

Marker attribute to exclude a property from automatic @AppStorage generation.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages