Solution for Ch 4 Bronze Challenge in Xcode 9.3

extension ConversionViewController: UITextFieldDelegate {
  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let hasAtMostOneDecimalSeparator = textField.text?.contains(".") == false || !string.contains(".")
    let validInputCharacterSet = CharacterSet.decimalDigits.union(CharacterSet(charactersIn: "."))
    let areValidCharacters = CharacterSet(charactersIn: string).isSubset(of: validInputCharacterSet)
    return hasAtMostOneDecimalSeparator && areValidCharacters
  }
}
1 Like

My alternate solution (the relevant lines anyways):

    let invalidCharacters = CharacterSet(charactersIn: "0123456789.").inverted
    let badChars = string.rangeOfCharacter(from: invalidCharacters)
1 Like

my solution

let separator = CharacterSet(charactersIn: ".")
let decimalDigit = CharacterSet.decimalDigits.union(separator)
let givenString = string.addingPercentEncoding(withAllowedCharacters: decimalDigit)

    if string == givenString {
        return true
    } else {
        return false
    }