Reputation: 1647
I am using userdefaults to save a decimal value of unknown length ( I.e. 51000000000) . Userdefaults does not allow for the saving of long decimal numbers, so I resorted to turning the value into a string and saving it. Then I want to re load the number however I have been unable to find a way to convert string value into a decimal. How do I do this?
Edit:
Code for setting the value into userdefaults:
let savedValues = UserDefaults.standard
savedValues.setValue(String(describing: buildingConstants.jitterClickConstantCost), forKey:"jitterClickCost")
struct buildingConstants {
static var jitterClickConstantCost = Decimal()
}
Upvotes: 15
Views: 30470
Reputation: 5259
You can try the default initializer for Decimal with a string parameter
here is an example
let decimal: Decimal = 51000000000
let str = String(describing: decimal)
let savedValues = UserDefaults.standard
savedValues.set(str, forKey:"jitterClickCost")
savedValues.synchronize()
let value = savedValues.string(forKey: "jitterClickCost") ?? "0"
let result = Decimal(string: value)
Upvotes: 29