This is very easy API to use to easily access and set userDefaults value in Swift and it supports the new privacy rules from apple.

What's New



What's Changed

  • Update UserDefaults.swift by @EngOmarElsayed in #6
    Solving a crash happens if an optional value was set to nil

Full Changelog: 1.1.0...1.2.0


example workflow GitHub License SPM compatible

Table of Contents

  1. Introduction
  2. How to use
  3. Storing Custome Data
  4. Author


This package is crafted for effortless integration and streamlined access to user defaults plus it supports the new privacy rules from WWDC23. Adding it to your project is straightforward: simply include the package link in the main app package like this:"

Screenshot 2024-03-15 at 3 28 47 AM

Alternatively, navigate to the top section labeled 'Files' and click on 'Add Package Dependency':

Screenshot 2024-03-15 at 3 33 08 AM

Next, paste the repository link into the search field and click on 'Add Package':

Screenshot 2024-03-15 at 3 34 52 AM

How to use

Utilizing this API is designed to be straightforward and as effortless as using the @AppStorage API. Simply add this property wrapper to your variable and provide it with a key - that's all there is to it

@UserDefaults(key: "previewShown") var previewShown = false // false is the default value

To help you better organize your UserDefaults keys, I've created an enum called DefaultKeys to store them. Just extend the enum and add new keys as static properties like this:

extension DefaultKeys {
  static let previewShown = "previewShown"


You can also store optional values just like that:

@UserDefaults(key: "previewShown") var previewShown: Bool?

Custome Container

You can also store your userDefaults in a custome container like so:

@UserDefaults(key: "previewShown", .init(suiteName: "YourCustomeContainerName")) var previewShown = false

Supported Data

SwiftUserDefaults supports all of the standard NSUserDefaults types, like String, Int, Bool, Array and much more.

Here's a full table of built-in single value defaults:

Single value Array
String [String]
Int [Int]
Double [Double]
Bool [Bool]
Data [Data]
Date [Date]
[String: Any] [[String: Any]]

But you can also support custome data types 🚀

Storing Custome Data

Storing custom data types is straightforward; you only need to ensure that the custom data type conforms to DefaultsCustomDataType, as demonstrated below:

struct CustomeData: DefaultsCustomDataType {
@UserDefaults(key: "customeData") var customeData = CustomeData()

DefaultsCustomDataType is a public protocol that conforms to the Codable protocol. It serves as a bridge, allowing the API to easily detect whether this data type needs to be encoded or not.

Example 1 Enums

Storing an Enum is straightforward; simply ensure that the Enum conforms to DefaultsCustomDataType, as shown below:

enum CustomeData: DefaultsCustomDataType {
case none
@UserDefaults(key: "customeData") var customeData: CustomeData = .none

Example 2 Custom Array type

Storing custom Array types is even simpler just ensure that the Element type of the Array conforms to DefaultsCustomDataType, as demonstrated below:

struct CustomeData: DefaultsCustomDataType {
@UserDefaults(key: "customeData") var customeData: [CustomeData] = [CustomeData()]

And that's all there is to it! 🚀 Enjoy using this Swifty package.


This pacakge was created by Eng.Omar Elsayed to helpe the iOS comuntity and make there life easir. To contact me email me at


  • Swift Tools 5.7.0
View More Packages from this Author


  • None
Last updated: Tue Apr 16 2024 11:48:09 GMT-0900 (Hawaii-Aleutian Daylight Time)