Bronze Challenge - Can I get some help understanding this?


#21

In keeping with the style of the book already put in place, here is my solution:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let existingTextHasDecimalSeparator = textField.text?.rangeOfString(".")
let replacementTextHasDecimalSeparator = string.rangeOfString(".")
let letters = NSCharacterSet.letterCharacterSet()
let replacementTextHasLetter = string.lowercaseString.rangeOfCharacterFromSet(letters)

    if existingTextHasDecimalSeparator != nil && replacementTextHasDecimalSeparator != nil || replacementTextHasLetter != nil {
        return false
    }
    else {
        return true
    }
}

#22

Hi emanleet,

Here is my function, I just wrote

func textField(textField: UITextField,
                   shouldChangeCharactersInRange range: NSRange,
                                                 replacementString string: String) -> Bool {
        
        print("Current text: \(textField.text)")
        print("Replacement text: \(string)")
        
        let existingTextHasDecimalSeparator = textField.text?.rangeOfString(".")
        let replacementTextHasDecimalSeparator = string.rangeOfString(".")
        
        
        
        if existingTextHasDecimalSeparator != nil && replacementTextHasDecimalSeparator != nil 

**|| string.rangeOfCharacterFromSet(NSCharacterSet.letterCharacterSet()) != nil**

 {
            return false
        }
        else {
            return true
        }
       
    }

and it works well !


#23

Here’s a nice, concise, and readable way to do this using a single if/else. Someone else posted the use of both the && and || operators, but I just prefer to use extraneous parentheses for the sake of readability since it’s easy to miss the grouping when you’re debugging whilst low on sleep :slight_smile:

func textField(textField: UITextField,
            shouldChangeCharactersInRange range: NSRange,
            replacementString string: String) -> Bool {
        let existingTextHasDecimalSeparator = textField.text?.rangeOfString(".")
        let replacementTextHasDecimalSeparator = string.rangeOfString(".")
        let letters = NSCharacterSet.letterCharacterSet()
        let replacementTextHasAlphabeticCharacters = string.rangeOfCharacterFromSet(letters)
        
        
        if (existingTextHasDecimalSeparator != nil &&
                replacementTextHasDecimalSeparator != nil) ||
                replacementTextHasAlphabeticCharacters != nil {
            return false
        }
        else {
            return true
        }
    }

#24

hello hello hello goodbye goodbye goodbye


#25
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    /*print("Current text: \(fTextField.text)")
    print("Replacement text: \(string)") //replacement text is the newly added text*/
    let lcEnglishRange = NSRange(location: 97, length: 26) //create an instance of NSRange class
    let lcEnglishLetters = NSCharacterSet(range:lcEnglishRange)
    let existingTextHasLetter=string.rangeOfCharacterFromSet(lcEnglishLetters)
    let existingTextHasDecimal=fTextField.text?.rangeOfString(".")
    let replacementTextHasDecimal=string.rangeOfString(".")
    if (existingTextHasDecimal != nil && replacementTextHasDecimal != nil) || existingTextHasLetter != nil
    {
        return false
    }
    else
    {
        return true
    }
}

#26

This was my solution, I am not taking in consideration special character:

    let letters = NSCharacterSet.letters
    let hasCharacters = string.rangeOfCharacter(from: letters)
    
    if hasCharacters != nil{
        return false
    }else{
        if existingTextHasDecimalSeparator != nil, replacementTextHasDecimalSeparator != nil{
            return false
        }else{
            return true
        }
    }