Page 75 more Swift 3 errors...The saga continues


#1

This is an extremely difficult time and honestly very frustrating to be doing this book with swift 3 coming out…I’m fairly new to Swift so the documentation doesn’t always make sense.

  1. Error: Type NumberFormatter.Style has no member ‘DecimalStyle’

let numberFormatter: NumberFormatter = {

    let nf = NumberFormatter()
    nf.numberStyle = .DecimalStyle
    nf.minimumFractionDigits = 0
    nf.maximumFractionDigits = 1
    return nf
    
    }()
  1. Error: expected expression on this line of code, “`celsiusLabel.text = numberFormatter.stringFromNumber(value)

func updateCelsiusLabel() {
if let value = celsiusValue {
`celsiusLabel.text = numberFormatter.stringFromNumber(value)

    }
    else {
        celsiusLabel.text = "???"
    }

}

any help would be greatly appreciated!


#2

Hey there - I had a hard time finding the new selectors for number style as well (“DecimalStyle” is now “decimal”.)

https://developer.apple.com/reference/foundation/numberformatter.style

As well, my code for the updateCelsuisLabel() function now looks like this:

func updateCelsiusLabel() {
    if let value = celsiusValue {
        celsiusLabel.text = numberFormatter.string(from: value as NSNumber)}
    else {
        celsiusLabel.text = "???"
    }
}

Although, I’m not sure that that’s the most elegant way to get there.


#3

thank you, I appreciate the reply!


#4

I had actually solved the issue by instantiating an instance of NSNumber:

celsiusLabel.text = numberFormatter.string(from: NSNumber.init(value: value))

But I like the way you did it better, so I believe your’s is the most elegant. :slight_smile:


#5

Solved this myself and ran into someone else having the same issue on StackOverflow. Solution is here: https://stackoverflow.com/questions/40429267/cannot-convert-value-of-type-double-to-expected-argument-type-nsnumber/40432131#40432131

Just for reference the application being built is ann exercise from the excellent book “IOS Programming: The Big Nerd Ranch Guide” chapter 4.

Problem 2 first: “Type ‘NumberFormatter.Style’ has no member ‘DecimalStyle’” Swift 3 or Xcode 8 have depreciated NSNumberFormatter for NumberFormatter and it makes the code a little easier to read. Your code should work this way

let numberFormatter: NumberFormatter = {
    let nf = NumberFormatter()
    nf.numberStyle = .decimal
    nf.minimumFractionDigits = 0
    nf.maximumFractionDigits = 1
    return nf
}()

Problem 1: Cannot convert value of type ‘Double’ to expected argument type ‘NSNumber’ Joel Bell has the correct answer. Here is my approach

func updateCelsiuslabel() {
    if let value = celsiusValue {
        celsiusLabel.text = numberFormatter.string(from: NSNumber(value: value))
    }
    else {
        celsiusLabel.text = "???"
    }
}

I hope that helps.


#6

yes it helps, and to avoid confusion :

func updateCelsiusLabel() {
if let result = celsiusValue {

        celsiusLabel.text = numberFormatter.string(from: NSNumber(value: result))
        
    }else{
           celsiusLabel.text = "???"
    }

}