Remove empty configs
A drop-in replacement for the iOS 14 @AppStorage property wrapper.
@AppStorage
UserDefaults
// iOS 14 @AppStorage("text") var text = "Default Text" @AppStorage("magic_number", store: .customUserDefaults) var magicNumber = 42 // iOS 13 @AppStorageCompat("text") var text = "Default Text" @AppStorageCompat("magic_number", store: .customUserDefaults) var magicNumber = 42
import SwiftUI import AppStorage enum StringEnum: String, Identifiable { case a, b, c var id: String { rawValue } } enum IntEnum: Int, Identifiable { case this, that, theOther var id: Int { rawValue } } struct ContentView: View { @AppStorageCompat("text", store: .standard) var text = "Default Text" @AppStorageCompat("string_enum") var selectionString: StringEnum = .a @AppStorageCompat("int_enum") var selectionInt: IntEnum = .this var body: some View { List { Section(header: Text("Acts like a persistent @State")) { TextField("Change me", text: $text) TextField("Change me, too!", text: $text) } Section(header: Text("Change UserDefaults without property wrapper")) { Button("Sneakily change a UserDefault") { UserDefaults.standard.setValue("One more thing...", forKey: "text") } Button("Remove a UserDefault") { UserDefaults.standard.setValue(nil, forKey: "text") } } Section(header: Text("Enums with raw values")) { Picker("Pick Me", selection: $selectionString) { Text("a").tag(StringEnum.a) Text("b").tag(StringEnum.b) Text("c").tag(StringEnum.c) }.pickerStyle(SegmentedPickerStyle()) Picker("Pick Me", selection: $selectionInt) { Text("this").tag(IntEnum.this) Text("that").tag(IntEnum.that) Text("the other").tag(IntEnum.theOther) }.pickerStyle(SegmentedPickerStyle()) } }.listStyle(GroupedListStyle()) } }
Add the package to your Package.swift file:
dependencies: [ .package(url: "https://github.com/xavierLowmiller/AppStorage.git", .upToNextMajor(from: "1.0.4")) ]
Add the pod to your Podfile:
platform :ios, '13.0' use_frameworks! target 'MyApp' do pod 'AppStorage', '~> 1.0.4' end
Add this line to your Cartfile:
github "xavierLowmiller/AppStorage" ~> 1.0.4
Since it’s just a single file, you can just download and drag it to your project.
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
AppStorage
A drop-in replacement for the iOS 14
@AppStorage
property wrapper.Features
@AppStorage
property wrapperUserDefaults
changesUsage
More complete example
Installation
Swift Package Manager
Add the package to your Package.swift file:
CocoaPods
Add the pod to your Podfile:
Carthage
Add this line to your Cartfile:
Manual
Since it’s just a single file, you can just download and drag it to your project.