So I am trying to make a replacement function for the string class on Swift to conform to how it works on Python (string to be replaced, string to be replaced with, count to be replaced) and I found this extension by @rmaddy that does that, however when I add it to my XCode project I get the error:
Referencing initializer 'init(_:)' on 'Range' requires that 'String.Index' conform to 'Strideable'
//Developed by rmaddy @ https://stackoverflow.com/users/2303865/rmaddy
extension String {
func replacingOccurrences<Target, Replacement>(of target: Target, with replacement: Replacement, count: Int, options: String.CompareOptions = [], range searchRange: Range<String.Index>? = nil) -> String where Target : StringProtocol, Replacement : StringProtocol {
var matches = [Range<String.Index>]()
//This is where I get the error
var sRange = searchRange ?? Range(startIndex..<endIndex)
while matches.count < count && !sRange.isEmpty {
if let mRange = range(of: target, options: options, range: sRange, locale: nil) {
matches.append(mRange)
if options.contains(.backwards) {
sRange = Range(sRange.lowerBound..<mRange.lowerBound)
} else {
sRange = Range(mRange.upperBound..<sRange.upperBound)
}
} else {
break
}
}
var res = self
for range in matches.sorted(by: { $0.lowerBound > $1.lowerBound }) {
res.replaceSubrange(range, with: replacement)
}
return res
}
}
I've tried going through the Swift documentation but can't really find how to fix this, please help.
To be clear I want to replace a number of occurrences of a word within a text file (read as a script) but not all. I am not using the find and replace function because I want it to be dynamic and to be able to pick the replacement word based on an algorithm. Basically I'm making a thing that changes occurrences of "said" with "exclaimed" and other synonyms. The project is a paraphrasing engine for long text files, it works easy on Python but rewriting it on Swift is proving very challenging.
Thanks for your time, people.