user2924482 user2924482 - 1 month ago 30
Objective-C Question

Swift 3: stringByReplacingCharactersInRange alternatives

Any of you knows an

stringByReplacingCharactersInRange
to be use in Swift 3?

I'm trying to convert this
Objective-C
to
Swift
:

strToSort = [strToSort stringByReplacingCharactersInRange:NSMakeRange((i-1),1) withString: [strToSort substringWithRange:NSMakeRange((i),1)]];


I'll really appreciate your help.

Answer

In Swift 3 it is.

strToSort = strToSort.replacingCharacters(in: range, with: str)

Note: Here range is type of Swift Range<String.Index> object not NSRange.

For more details on Range check Apple Documentation.

Comments