比较来自世界各地的卖家的域名和 IT 服务价格

"取代索引" 不可用:不能没有替换行 CountableClosedRange , 请参阅关于讨论文档的评论

在 Swift 4 我试图采取时收到此错误
Substring


String

, 使用替换索引的语法。

'subscript' 不可用:可以没有分类索引的行 CountableClosedRange, 请参阅关于讨论文档的评论

例如:


let myString: String = "foobar"
let mySubstring: Substring = myString[1..<3]


两个问题:

如何消除此错误?

哪里 "the documentation comment for discussion", 什么错误?
已邀请:

喜特乐

赞同来自:

如果要使用字符串类型的索引
"palindrome"[1..<3]


"palindrome"[1...3]

, 使用这些扩展。

Swift 4


extension String {
subscript /bounds: CountableClosedRange<int>/ -&gt; String {
let start = index/startIndex, offsetBy: bounds.lowerBound/
let end = index/startIndex, offsetBy: bounds.upperBound/
return String/self[start...end]/
}

subscript /bounds: CountableRange<int>/ -&gt; String {
let start = index/startIndex, offsetBy: bounds.lowerBound/
let end = index/startIndex, offsetBy: bounds.upperBound/
return String/self[start..<end] 'character'="" ,="" .="" 3="" 64="" 8="" <="" [code]return="" [code]string.index[="" [url]https:="" apple="" b8401e1fde52d95e5a8ce7b043a3c5a3bcf72181="" blob="" code]="" conceptual="" content="" core="" developer.apple.com="" div="" documentation="" error.="" github.com="" library="" public="" self[start...end][="" self[start..<end][="" stdlib="" stringsandcharacters.html[="" swift="" swift,="" swift_programming_language="" unavailablestringapis.swift.gyb#l15[="" url]="" utf-16.="" xcode="" }="" }[="" 为了="" 到="" 取决于字符串的编码方式。="" 和="" 因为定义="" 您可以指定其他字符串编码="" 替换="" 没有用语言集成它="" 符号可以来自="" 这是指的="" 钻头,默认情况下通常使用="">
<div class="answer_text">
你的问题 /和自我响应/ 它有 2 问题:

订阅字符串
[code]Int

从未在标准库中提供 Swift. 此代码无效,直到存在 Swift:


let mySubstring: Substring = myString[1..&lt;3]


新的
String.Index/encodedOffset: /

返回编码索引 UTF-16 /第16页/. 线 Swift 用途

高级集群移植

, 这可能需要 8 到 64 用于存储符号的位。 emodeji制作一个非常好的示威:


let myString = ""
let lowerBound = String.Index/encodedOffset: 1/
let upperBound = String.Index/encodedOffset: 3/
let mySubstring = myString[lowerBound..<upperbound] 1="" 3="" 4,="" :="" <="" [="" [code]let="" [code]string.index[="" actual="" and="" canadian="" code]="" div="" expected:="" flags="" gibberish="" let="" lowerbound="myString.index/myString.startIndex," mystring="" mysubstring="" offsetby:="" print="" swift="" uk="" upperbound="myString.index/myString.startIndex," 为了更好或更糟:="" 完全没有改变="" 实际上得到了="">
<div class="answer_text">
如何消除此错误?

此错误意味着您无法使用 Int 以索引格式 – 你必须使用 String.Index, 您可以初始化 encodedOffset Int.


[code]let myString: String = "foobar"
let lowerBound = String.Index.init/encodedOffset: 1/
let upperBound = String.Index.init/encodedOffset: 3/
let mySubstring: Substring = myString[lowerBound..<upperbound][ "the="" "小心豹子".="" <="" [url]https:="" apple="" b8401e1fde52d95e5a8ce7b043a3c5a3bcf72181="" blob="" code]="" comment="" core="" discussion",="" div="" documentation="" for="" github="" github.com="" public="" stdlib="" swift="" unavailablestringapis.swift.gyb="" unavailablestringapis.swift.gyb#l15[="" url]="" 什么错误?="" 他在="" 哪里="" 在文件中调用="" 在标准库存储中="" 在锁着的cartoison橱柜的底部,卡在被遗弃的厕所里面有一个标志的厕所:="">
<div class="answer_text">
您可以简单地将字符串转换为一个字符数组...


[code]let aryChar = Array/myString/


然后你将获得数组的所有功能......
</div>
<div class="answer_text">
基于答案



, 以下是两个扩展,可防止在线的开头和结尾之外的无效索引 /这些扩展还避免了从一开始的重新扫描字符串,只能在范围内找到索引/:


extension String {

subscript/bounds: CountableClosedRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count-1/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i...j]/
}

subscript/bounds: CountableRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i..<j] <="" code]="" div="" }="" }[="">
<div class="answer_text">
基于

Swift 4


[code]extension StringProtocol {
subscript/bounds: CountableClosedRange<int>/ -&gt; SubSequence {
let start = index/startIndex, offsetBy: bounds.lowerBound/
let end = index/start, offsetBy: bounds.count/
return self[start..<end] bounds:="" countablerange<int="" subscript="" }="">/ -&gt; SubSequence {
let start = index/startIndex, offsetBy: bounds.lowerBound/
let end = index/start, offsetBy: bounds.count/
return self[start..<end] ,="" -="" .="" <="" [code]index[="" [code]string[="" [code]stringprotocol[="" [code]substring[="" code]="" div="" i.="" n="" o="" }="" }[="" 两次。="" 从哪里开始="" 值得注意的变化:="" 它="" 方法="" 最终索引从边界的初始索引转移,而不是从行的开始。="" 现在扩张="" 获取这些索引。="" 这允许养父母,如="" 这可以防止从一开始就重新转换。="">
<div class="answer_text">

[code]extension String {

subscript/bounds: CountableClosedRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count-1/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i...j]/
}

subscript/bounds: CountableRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

***let upperBound = min/bounds.upperBound, self.count-1/***
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i..<j] <="" code]="" div="" }="" }[="">
<div class="answer_text">
您收到此错误,因为索引具有范围的索引结果
掠夺
[code]Substring?

, 但不是
Substring

.

您必须使用以下代码:


let myString: String = "foobar"
let mySubstring: Substring? = myString[1..&lt;3]


</div>
</j]></int></int></div></end]></end]></int></div></j]></int></int></div></upperbound][></div></upperbound]></div></end]></int></int>

董宝中

赞同来自:

你的问题 /和自我响应/ 它有 2 问题:

订阅字符串
Int

从未在标准库中提供 Swift. 此代码无效,直到存在 Swift:


let mySubstring: Substring = myString[1..<3]


新的
String.Index/encodedOffset: /

返回编码索引 UTF-16 /第16页/. 线 Swift 用途

高级集群移植

, 这可能需要 8 到 64 用于存储符号的位。 emodeji制作一个非常好的示威:


let myString = ""
let lowerBound = String.Index/encodedOffset: 1/
let upperBound = String.Index/encodedOffset: 3/
let mySubstring = myString[lowerBound..<upperbound] 1="" 3="" 4,="" :="" <="" [="" [code]let="" [code]string.index[="" actual="" and="" canadian="" code]="" div="" expected:="" flags="" gibberish="" let="" lowerbound="myString.index/myString.startIndex," mystring="" mysubstring="" offsetby:="" print="" swift="" uk="" upperbound="myString.index/myString.startIndex," 为了更好或更糟:="" 完全没有改变="" 实际上得到了="">
<div class="answer_text">
如何消除此错误?

此错误意味着您无法使用 Int 以索引格式 – 你必须使用 String.Index, 您可以初始化 encodedOffset Int.


[code]let myString: String = "foobar"
let lowerBound = String.Index.init/encodedOffset: 1/
let upperBound = String.Index.init/encodedOffset: 3/
let mySubstring: Substring = myString[lowerBound..<upperbound][ "the="" "小心豹子".="" <="" [url]https:="" apple="" b8401e1fde52d95e5a8ce7b043a3c5a3bcf72181="" blob="" code]="" comment="" core="" discussion",="" div="" documentation="" for="" github="" github.com="" public="" stdlib="" swift="" unavailablestringapis.swift.gyb="" unavailablestringapis.swift.gyb#l15[="" url]="" 什么错误?="" 他在="" 哪里="" 在文件中调用="" 在标准库存储中="" 在锁着的cartoison橱柜的底部,卡在被遗弃的厕所里面有一个标志的厕所:="">
<div class="answer_text">
您可以简单地将字符串转换为一个字符数组...


[code]let aryChar = Array/myString/


然后你将获得数组的所有功能......
</div>
<div class="answer_text">
基于答案



, 以下是两个扩展,可防止在线的开头和结尾之外的无效索引 /这些扩展还避免了从一开始的重新扫描字符串,只能在范围内找到索引/:


extension String {

subscript/bounds: CountableClosedRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count-1/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i...j]/
}

subscript/bounds: CountableRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i..<j] <="" code]="" div="" }="" }[="">
<div class="answer_text">
基于

Swift 4


[code]extension StringProtocol {
subscript/bounds: CountableClosedRange<int>/ -&gt; SubSequence {
let start = index/startIndex, offsetBy: bounds.lowerBound/
let end = index/start, offsetBy: bounds.count/
return self[start..<end] bounds:="" countablerange<int="" subscript="" }="">/ -&gt; SubSequence {
let start = index/startIndex, offsetBy: bounds.lowerBound/
let end = index/start, offsetBy: bounds.count/
return self[start..<end] ,="" -="" .="" <="" [code]index[="" [code]string[="" [code]stringprotocol[="" [code]substring[="" code]="" div="" i.="" n="" o="" }="" }[="" 两次。="" 从哪里开始="" 值得注意的变化:="" 它="" 方法="" 最终索引从边界的初始索引转移,而不是从行的开始。="" 现在扩张="" 获取这些索引。="" 这允许养父母,如="" 这可以防止从一开始就重新转换。="">
<div class="answer_text">

[code]extension String {

subscript/bounds: CountableClosedRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count-1/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i...j]/
}

subscript/bounds: CountableRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

***let upperBound = min/bounds.upperBound, self.count-1/***
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i..<j] <="" code]="" div="" }="" }[="">
<div class="answer_text">
您收到此错误,因为索引具有范围的索引结果
掠夺
[code]Substring?

, 但不是
Substring

.

您必须使用以下代码:


let myString: String = "foobar"
let mySubstring: Substring? = myString[1..&lt;3]


</div>
</j]></int></int></div></end]></end]></int></div></j]></int></int></div></upperbound][></div></upperbound]>

三叔

赞同来自:

如何消除此错误?

此错误意味着您无法使用 Int 以索引格式 – 你必须使用 String.Index, 您可以初始化 encodedOffset Int.


let myString: String = "foobar"
let lowerBound = String.Index.init/encodedOffset: 1/
let upperBound = String.Index.init/encodedOffset: 3/
let mySubstring: Substring = myString[lowerBound..<upperbound][ "the="" "小心豹子".="" <="" [url]https:="" apple="" b8401e1fde52d95e5a8ce7b043a3c5a3bcf72181="" blob="" code]="" comment="" core="" discussion",="" div="" documentation="" for="" github="" github.com="" public="" stdlib="" swift="" unavailablestringapis.swift.gyb="" unavailablestringapis.swift.gyb#l15[="" url]="" 什么错误?="" 他在="" 哪里="" 在文件中调用="" 在标准库存储中="" 在锁着的cartoison橱柜的底部,卡在被遗弃的厕所里面有一个标志的厕所:="">
<div class="answer_text">
您可以简单地将字符串转换为一个字符数组...


[code]let aryChar = Array/myString/


然后你将获得数组的所有功能......
</div>
<div class="answer_text">
基于答案



, 以下是两个扩展,可防止在线的开头和结尾之外的无效索引 /这些扩展还避免了从一开始的重新扫描字符串,只能在范围内找到索引/:


extension String {

subscript/bounds: CountableClosedRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count-1/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i...j]/
}

subscript/bounds: CountableRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i..<j] <="" code]="" div="" }="" }[="">
<div class="answer_text">
基于

Swift 4


[code]extension StringProtocol {
subscript/bounds: CountableClosedRange<int>/ -&gt; SubSequence {
let start = index/startIndex, offsetBy: bounds.lowerBound/
let end = index/start, offsetBy: bounds.count/
return self[start..<end] bounds:="" countablerange<int="" subscript="" }="">/ -&gt; SubSequence {
let start = index/startIndex, offsetBy: bounds.lowerBound/
let end = index/start, offsetBy: bounds.count/
return self[start..<end] ,="" -="" .="" <="" [code]index[="" [code]string[="" [code]stringprotocol[="" [code]substring[="" code]="" div="" i.="" n="" o="" }="" }[="" 两次。="" 从哪里开始="" 值得注意的变化:="" 它="" 方法="" 最终索引从边界的初始索引转移,而不是从行的开始。="" 现在扩张="" 获取这些索引。="" 这允许养父母,如="" 这可以防止从一开始就重新转换。="">
<div class="answer_text">

[code]extension String {

subscript/bounds: CountableClosedRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count-1/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i...j]/
}

subscript/bounds: CountableRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

***let upperBound = min/bounds.upperBound, self.count-1/***
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i..<j] <="" code]="" div="" }="" }[="">
<div class="answer_text">
您收到此错误,因为索引具有范围的索引结果
掠夺
[code]Substring?

, 但不是
Substring

.

您必须使用以下代码:


let myString: String = "foobar"
let mySubstring: Substring? = myString[1..&lt;3]


</div>
</j]></int></int></div></end]></end]></int></div></j]></int></int></div></upperbound][>

龙天

赞同来自:

您可以简单地将字符串转换为一个字符数组...


let aryChar = Array/myString/


然后你将获得数组的所有功能......

董宝中

赞同来自:

基于答案



, 以下是两个扩展,可防止在线的开头和结尾之外的无效索引 /这些扩展还避免了从一开始的重新扫描字符串,只能在范围内找到索引/:


extension String {

subscript/bounds: CountableClosedRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count-1/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i...j]/
}

subscript/bounds: CountableRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i..<j] <="" code]="" div="" }="" }[="">
<div class="answer_text">
基于

Swift 4


[code]extension StringProtocol {
subscript/bounds: CountableClosedRange<int>/ -&gt; SubSequence {
let start = index/startIndex, offsetBy: bounds.lowerBound/
let end = index/start, offsetBy: bounds.count/
return self[start..<end] bounds:="" countablerange<int="" subscript="" }="">/ -&gt; SubSequence {
let start = index/startIndex, offsetBy: bounds.lowerBound/
let end = index/start, offsetBy: bounds.count/
return self[start..<end] ,="" -="" .="" <="" [code]index[="" [code]string[="" [code]stringprotocol[="" [code]substring[="" code]="" div="" i.="" n="" o="" }="" }[="" 两次。="" 从哪里开始="" 值得注意的变化:="" 它="" 方法="" 最终索引从边界的初始索引转移,而不是从行的开始。="" 现在扩张="" 获取这些索引。="" 这允许养父母,如="" 这可以防止从一开始就重新转换。="">
<div class="answer_text">

[code]extension String {

subscript/bounds: CountableClosedRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count-1/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i...j]/
}

subscript/bounds: CountableRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

***let upperBound = min/bounds.upperBound, self.count-1/***
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i..<j] <="" code]="" div="" }="" }[="">
<div class="answer_text">
您收到此错误,因为索引具有范围的索引结果
掠夺
[code]Substring?

, 但不是
Substring

.

您必须使用以下代码:


let myString: String = "foobar"
let mySubstring: Substring? = myString[1..&lt;3]


</div>
</j]></int></int></div></end]></end]></int></div></j]></int></int>

喜特乐

赞同来自:

基于

Swift 4


extension StringProtocol {
subscript/bounds: CountableClosedRange<int>/ -&gt; SubSequence {
let start = index/startIndex, offsetBy: bounds.lowerBound/
let end = index/start, offsetBy: bounds.count/
return self[start..<end] bounds:="" countablerange<int="" subscript="" }="">/ -&gt; SubSequence {
let start = index/startIndex, offsetBy: bounds.lowerBound/
let end = index/start, offsetBy: bounds.count/
return self[start..<end] ,="" -="" .="" <="" [code]index[="" [code]string[="" [code]stringprotocol[="" [code]substring[="" code]="" div="" i.="" n="" o="" }="" }[="" 两次。="" 从哪里开始="" 值得注意的变化:="" 它="" 方法="" 最终索引从边界的初始索引转移,而不是从行的开始。="" 现在扩张="" 获取这些索引。="" 这允许养父母,如="" 这可以防止从一开始就重新转换。="">
<div class="answer_text">

[code]extension String {

subscript/bounds: CountableClosedRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count-1/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i...j]/
}

subscript/bounds: CountableRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

***let upperBound = min/bounds.upperBound, self.count-1/***
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i..<j] <="" code]="" div="" }="" }[="">
<div class="answer_text">
您收到此错误,因为索引具有范围的索引结果
掠夺
[code]Substring?

, 但不是
Substring

.

您必须使用以下代码:


let myString: String = "foobar"
let mySubstring: Substring? = myString[1..&lt;3]


</div>
</j]></int></int></div></end]></end]></int>

快网

赞同来自:

extension String {

subscript/bounds: CountableClosedRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

let upperBound = min/bounds.upperBound, self.count-1/
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i...j]/
}

subscript/bounds: CountableRange<int>/ -&gt; String {
let lowerBound = max/0, bounds.lowerBound/
guard lowerBound &lt; self.count else { return "" }

***let upperBound = min/bounds.upperBound, self.count-1/***
guard upperBound &gt;= 0 else { return "" }

let i = index/startIndex, offsetBy: lowerBound/
let j = index/i, offsetBy: upperBound-lowerBound/

return String/self[i..<j] <="" code]="" div="" }="" }[="">
<div class="answer_text">
您收到此错误,因为索引具有范围的索引结果
掠夺
[code]Substring?

, 但不是
Substring

.

您必须使用以下代码:


let myString: String = "foobar"
let mySubstring: Substring? = myString[1..&lt;3]


</div>
</j]></int></int>

郭文康

赞同来自:

您收到此错误,因为索引具有范围的索引结果
掠夺
Substring?

, 但不是
Substring

.

您必须使用以下代码:


let myString: String = "foobar"
let mySubstring: Substring? = myString[1..<3]

要回复问题请先登录注册