Cannot assign a value of type nsnumber to a value type string

2k views Asked by At

I'm using number formatter to change a value number from a JSON to a number with commas (e.g. 500000 to 500,000). I think I have it down but I'm getting an error saying:

cannot assign a value of type nsnumber to a value type string

if let supply = json["supply"] as? NSNumber{
    var numberFormatter = NSNumberFormatter()
    numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
    numberFormatter.stringFromNumber(supply)!
    totalXRP.text = supply

}
1

There are 1 answers

5
Eric Aya On

You have to assign the result of numberFormatter.stringFromNumber to your text field:

if let supply = json["supply"] as? NSNumber{
    var numberFormatter = NSNumberFormatter()
    numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
    if let result = numberFormatter.stringFromNumber(supply) {
        totalXRP.text = result
    }
}