AppStorage vs CoreData

Viewed 364

Recent newbie to SwiftUI and was noticed the introduction of property wrapper @AppStorage in iOS 14. Wondering about the difference between @AppStorage and CoreData

struct ContentView: View {
  @AppStorage("isDarkMode") 
  private var isDarkMode: Bool = false

  var body: some View {
    VStack {
      Text(isDarkMode ? "Dark" : "Light")

      Toggle(isOn: $isDarkMode) {
        Text("Switch Mode")
      }.fixedSize()
    }
  }
}
1 Answers

AppStorage is a property-wrapper around UserDefaults for SwiftUI. So whatever you're storing using AppStorage is available also via UserDefaults.

@AppStorage("isDarkMode") private var isDarkMode: Bool = false
//...
let isDarkMode = UserDefaults.standard.bool(forKey: "isDarkMode")

CoreData is where you store large amounts of data. You can go through this post.

Related