Проблема в том, что replacingOccurrencesOf
- это метод NSString Какао Objective-C, поэтому в итоге возникает несоответствие импеданса типа между понятием String диапазона и понятием NSString NSRange. Самое простое решение - остаться в мире NSString:
label.text = (title as NSString).replacingOccurrences(
of: "\n", with: "", options: .caseInsensitive,
range: NSRange(location: 0, length: 2))
В противном случае я согласен с идеей Vacawama о расширении:
extension String {
func range(_ start:Int, _ count:Int) -> Range<String.Index> {
let i = self.index(start >= 0 ?
self.startIndex :
self.endIndex, offsetBy: start)
let j = self.index(i, offsetBy: count)
return i..<j
}
func nsRange(_ start:Int, _ count:Int) -> NSRange {
return NSRange(self.range(start,count), in:self)
}
}
Тогда вы можете сказать
label.text = title.replacingOccurrences(
of: "\n", with: "", options: .caseInsensitive,
range: title.range(0,2))