user2924482 user2924482 - 1 year ago 414
Objective-C Question

Swift 3: stringByReplacingCharactersInRange alternatives

Any of you knows an

to be use in Swift 3?

I'm trying to convert this

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

I'll really appreciate your help.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download