Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using stringByReplacingCharactersInRange in Swift

Tags:

I'm trying to use UITextFieldDelegate in Swift/Xcode6 and I'm struggling with the way I'm supposed to use stringByReplacingCharactersInRange. The compiler error is 'Cannot convert the expression's type 'String' to type '$T8'.

func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool
{
    let s = textField.text.stringByReplacingCharactersInRange(range:range, withString:string)
    if countElements(s) > 0 {

    } else {

    }
    return true
}

Update for Xcode 6 Beta 5: The thing is shouldChangeCharactersInRange gives an NSRange object and we'd need a Swift Range object for stringByReplacingCharactersInRange. Can this still be considered a bug as I don't see why we should still be dealing with NS* objects? The String argument of the delegate method is anyway of a Swift type.

like image 326
Seppo Avatar asked Jun 03 '14 12:06

Seppo


2 Answers

Here's how to calculate the resulting string in various Swift versions.

Note that all methods use -[NSString stringByReplacingOccurrencesOfString:withString:] in exactly the same way, just differing in syntax.

This is the preferred way to calculate the resulting string. Converting to a Swift Range and use that on a Swift String is error prone. Johan's answer for example is incorrect in a couple of ways when operating on non-ASCII strings.

Swift 3:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let result = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) ?? string
    // ... do something with `result`
}

Swift 2.1:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let result = (textField.text as NSString?)?.stringByReplacingCharactersInRange(range, withString: string)
    // ... do something with `result`
}

Swift 1 (only left here for reference):

let result = textField.text.bridgeToObjectiveC().stringByReplacingCharactersInRange(range, withString:string)
like image 145
Nikolai Ruhe Avatar answered Oct 06 '22 15:10

Nikolai Ruhe


I created an extension to NSRange the converted to Range<String.Index>

extension NSRange {
    func toRange(string: String) -> Range<String.Index> {
        let startIndex = advance(string.startIndex, location)
        let endIndex = advance(startIndex, length)
        return startIndex..<endIndex
    }
}

So I can create the String like this

let text = textField.text
let newText = text.stringByReplacingCharactersInRange(range.toRange(text), withString: string)

in Swift 2.1 the extension looks like:

extension NSRange {
    func toRange(string: String) -> Range<String.Index> {
        let startIndex = string.startIndex.advancedBy(location)
        let endIndex = startIndex.advancedBy(length)
        return startIndex..<endIndex
    }
}
like image 37
Johan Avatar answered Oct 06 '22 16:10

Johan