Swift 3 Conversion of Chapter 6

#1

When converting the code of WorldTrotter in Chapter 6 to Swift 3, I get the following compilation error on line 36-37:

Delegation/Solutions/WorldTrotter copy/WorldTrotter/ConversionViewController.swift:37:62: Argument labels ‘(_:)’ do not match any available overloads

Any idea how to fix this?

#2

Can you post the affected code?

By the way there is a method parameter label missing.

First parameter label in swift 3 is mandatory

#3

Hello.
I have a feeling this is coming from the addTarget method, but show us the code to be sure.

#4

Here is the code causing the error. It is in line # 36-37

import UIKit

class ConversionViewController: UIViewController, UITextFieldDelegate {

@IBOutlet var celsiusLabel: UILabel!
@IBOutlet var textField: UITextField!

var fahrenheitValue: Double? {
    didSet {
        updateCelsiusLabel()
    }
}

var celsiusValue: Double? {
    if let value = fahrenheitValue {
        return (value - 32) * (5/9)
    }
    else {
        return nil
    }
}

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

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

@IBAction func dismissKeyboard(_ sender: AnyObject) {
    textField.resignFirstResponder()
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    let existingTextHasDecimalSeparator = textField.text?.range(of: ".")
    let replacementTextHasDecimalSeparator = string.range(of: ".")
    
    // Provent letter entry
    let replacementStringHasLetters = string.rangeOfCharacter(from: CharacterSet.letters)
    
    if ((existingTextHasDecimalSeparator != nil && replacementTextHasDecimalSeparator != nil) || replacementStringHasLetters != nil) {
        return false
    }
    else {
        return true
    }
}

@IBAction func fahrenheitFieldEditingChanged(_ textField: UITextField) {
    if let text = textField.text, let value = Double(text) {
        fahrenheitValue = value
    }
    else {
        fahrenheitValue = nil
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    print ("ConversionViewController loaded its view.")
}

override func viewWillAppear(_ animated: Bool) {
    let hour = (Calendar.current as NSCalendar).component(NSCalendar.Unit.hour, from: Date())
    if (hour > 18 || hour < 6) {
        view.backgroundColor = UIColor.lightGray
    } else {
        view.backgroundColor = UIColor.white
    }
}

}

Thanks

#5

Hi,
It appears the ‘from:’ parameter name has been changed recently. It’s now called ‘for:’. This change isn’t reflected in the API Docs at the time of this reply.

Try re-typing that line (not copy & paste) and look at the suggested constructor when you call the .string method from your numberFormatter object. So the entire line of code giving you trouble should look like…

celciusLabel.text = numberFormat.string(for: value)

#6

Your recommended solution worked!

Thanks