How to store Dimensions in Swift

Viewed 46

I am working on a unit conversion app written in SwiftUI. How do I store Dimensions and make them persist when the app is closed. @State var inUnit: Dimension = UnitMass.kilograms I have searched around and can't find anything on storing or converting this to a string. All I need to store is the UnitMass.kilograms.

1 Answers

You can use UserDefaults and save rawValue inside onDisappear block.

var body: some View {
   VStack(spacing: 0) {
   //some code
   }
   .onDisappear {
      UserDefaults.standard.set(dimension.rawValue, forKey: "dimension")
   }
Related