How can I update the following code to the new version of swift:
self.areaCodeLael.text! = localNumber.substring(to: localNumber.index(localNumber.startIndex, offsetBy: 3))
I have tried following this post but I can't get it right How can I use String slicing subscripts in Swift 4?
I adjusted my original code to localNumber[..<3]
but I get:
Cannot subscript a value of type 'String' with an index of type 'PartialRangeUpTo'
In Swift 4 you slice a string into a substring using subscripting. The use of substring(from:) , substring(to:) and substring(with:) are all deprecated.
In Swift, the first property is used to return the first character of a string.
Swift String dropFirst() The dropFirst() method removes the first character of the string.
Substrings. When you get a substring from a string—for example, using a subscript or a method like prefix(_:) —the result is an instance of Substring , not another string. Substrings in Swift have most of the same methods as strings, which means you can work with substrings the same way you work with strings.
I would say localNumber.prefix(3)
in this situation. Short and sweet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With