不推荐使用“substring(to:)":请使用带有“partial range upto"运算符的字符串切片下标 [英] 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator
本文介绍了不推荐使用“substring(to:)":请使用带有“partial range upto"运算符的字符串切片下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将以下代码更新到新版本的 swift:
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))
我试过关注这篇文章,但我做对了如何在 Swift 4 中使用字符串切片下标?
I have tried following this post but I can't get it right How can I use String slicing subscripts in Swift 4?
我将原始代码调整为 localNumber[..<3]
但我得到:
I adjusted my original code to localNumber[..<3]
but I get:
不能使用类型为PartialRangeUpTo"的索引为String"类型的值添加下标
Cannot subscript a value of type 'String' with an index of type 'PartialRangeUpTo'
推荐答案
在这种情况下我会说 localNumber.prefix(3)
.简短而甜蜜.
I would say localNumber.prefix(3)
in this situation. Short and sweet.
这篇关于不推荐使用“substring(to:)":请使用带有“partial range upto"运算符的字符串切片下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文