Estoy usando este código para insertar guiones en una cadena para un mejor ajuste de palabras. Con Swift 5 obtengo la información de que
String.UTF16View.Index(encodedOffset: l)
estaría en desuso. Pero no puedo averiguar los parámetros correctos. ¿Algunas ideas?
import Foundation extension String { func hyphenated(languageCode: String) -> String { let locale = Locale(identifier: languageCode) return self.hyphenated(locale: locale) } func hyphenated(locale: Locale) -> String { guard CFStringIsHyphenationAvailableForLocale(locale as CFLocale) else { return self } var s = self let fullRange = CFRangeMake(0, s.utf16.count) var hyphenationLocations = [CFIndex]() for (i, _) in s.utf16.enumerated() { let location: CFIndex = CFStringGetHyphenationLocationBeforeIndex(s as CFString, i, fullRange, 0, locale as CFLocale, nil) if hyphenationLocations.last != location { hyphenationLocations.append(location) } } for l in hyphenationLocations.reversed() { guard l > 0 else { continue } let strIndex = String.UTF16View.Index(encodedOffset: l) // insert soft hyphen: s.insert("\u{00AD}", at: strIndex) // or insert a regular hyphen to debug: // s.insert("-", at: strIndex) } return s } }l es el desplazamiento en unidades de código UTF-16 en la cadena s , por lo que puede reemplazar
let strIndex = String.UTF16View.Index(encodedOffset: l)por
let strIndex = s.utf16.index(s.utf16.startIndex, offsetBy: l)o usar
let strIndex = String.Index(utf16Offset: l, in: s)que se introdujo en Swift 5 con SE-0241 Deprecate String Index Encoded Offsets .