Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 4-“子字符串(to:)”已弃用-我的代码的替代方案_Ios_Swift_Swift4 - Fatal编程技术网

Ios Swift 4-“子字符串(to:)”已弃用-我的代码的替代方案

Ios Swift 4-“子字符串(to:)”已弃用-我的代码的替代方案,ios,swift,swift4,Ios,Swift,Swift4,我的代码如下 var theReview = addReview.text let len2 = addReview.text.utf16.count if len2 > 3000 { theReview = theReview?.substring(to: (theReview?.index((theReview?.startIndex)!, offsetBy: 3000))!) } 我的目标是,如果文本长度超过3000个字符,则获取

我的代码如下

   var theReview = addReview.text
    let len2 = addReview.text.utf16.count

    if len2 > 3000 {

        theReview = theReview?.substring(to: (theReview?.index((theReview?.startIndex)!, offsetBy: 3000))!)

    }
我的目标是,如果文本长度超过3000个字符,则获取文本的前3000个字符

然而,我得到以下警告:

“substringto:”已弃用:请使用字符串切片下标 使用“部分范围高达”运算符

什么可以替代我的代码。我不是一个非常专业的程序员。因此,任何帮助都将是非常好的。

只需在视图中调用所需长度的前缀

func prefix_uuMaxLength:Int->Substring

返回一个子序列,最大长度为指定的最大长度,包含 集合的初始元素

如果最大长度超过 集合,则结果包含集合中的所有元素


注意:无需检查theReview的长度是否超过3000。它将由前缀本身处理。

这可能会对您有所帮助吗

var theReview = addReview.text
theReview = String(theReview.prefix(3000))

将字符串init与slice一起使用

让索引=theReview.indextheReview.startIndex,偏移量:3000
theReview=StringtheReview[theReview.startIndex..有关更好地使用prefix@JoakimDanielson是的,我看到了PGDev的答案,更好的答案,即使字符串长度小于3000。前缀仍然有效。@user28434是的,这就是为什么我们将其嵌入if中condition@NaqeebAhmed,我的意思是,如果不需要,在最坏的情况下,你会得到原始字符串。我猜这甚至可能是comppressed使theReview=StringaddReview.text。prefix3000@JoakimDanielson当然可以。但长度检查有问题。
var theReview = addReview.text
theReview = String(theReview.prefix(3000))