Codey Codey - 1 year ago 185
Swift Question

deleteCharactersInRange - Swift

I want do call the method "deleteCharactersInRange" but it doesn't work.

This is an expert from Apples Documentation


func deleteCharactersInRange(_ aRange: NSRange)

var day = ""

let stringRange:NSRange = NSMakeRange(0, 4)
day = day.deleteCharacterInRange(stringRange)

// I've also tried this, because in the Documentation
// I can't see wether the method is void or returns a String


I get this error message:

'String' does not have a member named 'deleteCharactersInRange'

Answer Source

The method you're citing belongs to NSMutableString. But since you're using Swift and haven't explicitly created one, you get a Swift String.

If you want to operate on Swift String, you need to use str.removeRange and the rather awkward to use Range:

var str = "Hello, playground"
str.removeRange(Range<String.Index>(start: str.startIndex, end:advance(str.startIndex, 7)))
// "playground"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download