Utilisez la méthode samePosition (in :) de String.Index avec la vue UTF16 de la chaîne. NSString utilise UTF16, donc les index UTF16 doivent être identiques aux index que NSString attend pour NSRange.
EDIT:
public extension NSRange {
private init(string: String, lowerBound: String.Index, upperBound: String.Index) {
let utf16 = string.utf16
let lowerBound = lowerBound.samePosition(in: utf16)
let location = utf16.distance(from: utf16.startIndex, to: lowerBound)
let length = utf16.distance(from: lowerBound, to: upperBound.samePosition(in: utf16))
self.init(location: location, length: length)
}
public init(range: Range<String.Index>, in string: String) {
self.init(string: string, lowerBound: range.lowerBound, upperBound: range.upperBound)
}
public init(range: ClosedRange<String.Index>, in string: String) {
self.init(string: string, lowerBound: range.lowerBound, upperBound: range.upperBound)
}
}
Dupe? [NSRange de Swift Range?] (Https://stackoverflow.com/q/27040924/2415822). Une raison pour laquelle vous ne voulez pas passer à NSString? En outre, il existe des solutions non-NSString dans les réponses à cette question. – JAL
La transition vers 'NSString' ne semble pas pure, intuitive, compréhensible, auto-documentée, lisible, simple, belle ou efficace. Aucune des solutions non-'NSString' de cette question ne fonctionne pour les chaînes contenant des emojis. – ma11hew28