smallpepperz
smallpepperz

Reputation: 1342

How can I store a float with @AppStorage with SwiftUI

I have a float (rate) that can be set by the user for this app. I would like to keep this variable persistent, so I would like to use the @AppStorage property wrapper. The problem I'm having is that @AppStorage("rate) var rate: Float = 0.5 gives No exact matches in call to initializer. After some brief googling, I learned that you cannot store floats with app storage. Is there a way to work around this?

Upvotes: 2

Views: 1429

Answers (1)

George
George

Reputation: 30401

Looking at the definition of AppStorage you can find the following allowed types:

  • Bool
  • Int
  • Double
  • String
  • URL
  • Data
  • enum with Int raw value
  • enum with String raw value
  • Bool?
  • Int?
  • Double?
  • String?
  • URL?
  • Data?
  • optional enum with Int raw value
  • optional enum with String raw value

So you should probably use Double, rather than Float (you shouldn't really use Float anyway, since it's 32-bit rather than 64-bit).

Upvotes: 2

Related Questions