yharshal/userdefaultswrapper
A Swift package that provides a convenient property wrapper for `UserDefaults`.
Installation
Add the package via Swift Package Manager:
// In Xcode: File > Add Packages > enter your repo URL
dependencies: [
.package(url: "https://github.com/yharshal/UserDefaultsWrapper.git", from: "1.0.0")
]Usage
Import package
import UserDefaultsWrapper
Define properties using the @UserDefault wrapper:
struct UserProfile {
@UserDefault(key: "username", defaultValue: "Guest")
static var username: String
@UserDefault(key: "launchCount", defaultValue: 0)
static var launchCount: Int
@UserDefault(key: "userRatings", defaultValue: 4.0)
static var userRatings: Double
@UserDefault(key: "userID", defaultValue: UUID())
static var userID: UUID
@UserDefault(key: "hasCompletedOnboarding", defaultValue: true)
static var hasCompletedOnboarding: Bool
}Access defaut value:
print("Welcome: \(UserProfile.username)") // Guest
Update Value:
UserProfile.username = "Harshal"
print("Welcome: \(UserProfile.username)") // Harshal
Running the Example
- This package includes an example executable target.
- Run it locally with:
```
swift run UserDefaultsWrapperExample
```
- It will demonstrate property wrapper in action
Package Metadata
Repository: yharshal/userdefaultswrapper
Default branch: main
README: README.md