'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator swift swift

'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator


I would say localNumber.prefix(3) in this situation. Short and sweet.


The problem with your attempt to use localNumber[..<3] is that you can't pass an Int as the range. You need to pass a String.Index.

Your code needs to be:

let index = localNumber.index(localNumber.startIndex, offsetBy: 3)self.areaCodeLael.text = localNumber[..<index]

Also note that you do not want the ! operator after text.