- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了一些奇怪的 Range
行为,现在我质疑我认为我知道的关于 Swift 中的 Range
的一切。
let range = Range<Int>(start: 0, end: 2)
print(range.count) // Prints 2
由于 Range
使用 start
和 end
而不是 location
和 length
NSRange
使用我希望上面的范围计数为 3。它几乎看起来像被视为 NSRange
因为如果计数为 2 是有意义的您的 location = 0
和 length = 2
。
let array = ["A", "B", "C", "D"]
let slice = array[range]
我希望 slice
包含 ABC
因为范围的结束索引是 2,但 slice
实际上包含 AB
,它确实对应于 range.count == 2
,但不会相加,因为范围的 endIndex == 2
应该包括 C
.
我在这里错过了什么?
我使用的是 Xcode 7.2 版本的 Swift,而不是任何开源版本。
最佳答案
范围对象 Range<T>
在 Swift 中,默认情况下,呈现为半开区间 [start,end)
,即 start..<end
(参见 HalfOpenInterval IntervalType
)。
如果你打印你的 range
就可以看到这个变量
let range = Range<Int>(start: 0, end: 2)
print(range) // 0..<2
此外,正如 Leo Dabus 在下面指出的(谢谢!),您可以简化 Range<Int>
的声明。通过使用半开范围运算符 ..<
直接:
let range = 0..<2
print(range) // 0..<2 (naturally)
同样,你可以声明Range<Int>
使用封闭范围运算符的范围 ...
let range = 0...1
print(range) // 0..<2
我们看到,有趣的是(在问题的上下文中),这再次验证了范围的默认表示是通过半开运算符。
半开间隔是 Range
的默认值在 language reference for range 的文本中有些含蓄地写着:
Like other collections, a range containing one element has an endIndex that is the successor of its startIndex; and an empty range has startIndex == endIndex.
Range
然而,符合 CollectionType
协议(protocol)。在 the language reference to the latter , 它清楚地表明 startIndex
和 endIndex
定义半开区间:
The sequence view of the elements is identical to the collection view. In other words, the following code binds the same series of values to x as does for x in self {}:
for i in startIndex..<endIndex {
let x = self[i]
}
把它包起来; Range
被定义为半开间隔 ( startIndex ..< endIndex
),即使在文档中很难找到它。
另见
Swift includes two range operators, which are shortcuts for expressing a range of values.
...
The closed range operator (a...b) defines a range that runs from a to b, and includes the values a and b. The value of a must not be greater than b.
...
The half-open range operator (a..< b) defines a range that runs from a to b, but does not include b. It is said to be half-open because it contains its first value, but not its final value.
关于Swift Range 表现得像 NSRange 并且不包括 endIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34539271/
我有以下代码 public static void main(String args[]) throws Exception { String mySite = "StackOverflow"
这些评论有道理吗?试图找出为什么它删除了我的反向给定字符串的字符,而不是本例中的常规给定字符串。 import Foundation extension String { func reverse
我正在寻找“Swift 3”处理错误的方法,我尝试将字符串的位置增加到越界索引。我有一个如下所示的扩展: extension String { func substring(from: Int)
如果您创建一个 var Range = 0...0,我希望 endIndex 为零。但实际上是1。 var myRange: Range = 0...0 print("start Index \(my
我有一个函数可以找到用户在 UITextView 中选择的当前单词。但是,如果我在 UITextView.text 属性中有表情符号时调用此函数,我会看到崩溃。我相信这是因为 String 与 NSS
我想要我正在制作的计算器,特别是退格按钮来检查点是否被删除 if mainNumberLine.text!.endIndex == "." { } 写下我遇到的最常见错误“表达式类型不明确,没有更多上
我在一个项目中使用了 substring,之前我使用的是 endindex 位置,但没有产生预期的结果,后来我才知道 java substring 执行 endindex-1。我发现它没有用。那么为什
我有一个如下所示的字符串 String s = "John21071989Bangalore"; Class Person { String name; Long dob; String
endIndex 返回与 count 相同的值。这是正确的行为还是错误? var ar = [1, 2, 3, 4] ar.count // 4 ar.endIndex // 4 最佳答案 count
问题基本上说明了一切。假设我有一个(排序的)列表,可以包含从1K到1M的任何项目。我有一个starting index和一个ending index。如果我使用ArrayList.sublist(st
我正在为 iOS 应用程序使用 Swift 2.0/Xcode 7.0。我已经构建了一个日语 IME 来将罗马字符转换为对应的日语。例如: 输入“a”会将“a”转换为“あ” 在键入下一个字符之前,键入
我遇到了一些奇怪的 Range 行为,现在我质疑我认为我知道的关于 Swift 中的 Range 的一切。 let range = Range(start: 0, end: 2) print(rang
我正在尝试使用自动微分库 Adept,我让它与 gcc 4.9.0 和 icc 16.0.2 一起工作,但在 VS 2017 和 Clang 4.0.1 上失败 我已将问题简化为以下片段,并且在我与库
出于某种原因我得到了 fatal error: can not increment endIndex 在 myRange 行。为什么我会收到此错误?我不明白我的代码有什么问题。 import UIKi
我想问一下是否有某种实用函数可以在提供索引的同时提供数组连接。也许jQuery的Prototype提供了这个,如果没有,我自己写:) 我期望的是这样的 var array= ["a", "b", "c
我有 UITextView 的扩展: extension UITextView { func separatedWordsInFrontCursor(infront: Bool = true)
endIndex 和 count 的 String 在 swift2 中怎么可能不同?这是我使用的代码示例。当所有字符都只有英文时,不会发生这种情况。 print("count:",self.
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: why from index is inclusive but end index is exclusive
我尝试获取字符串的 startIndex 和另一个索引之间的距离,但在第一个循环迭代中出现以下错误。该代码实际上适用于大多数字符串,但有些会崩溃。 fatal error: cannot increm
我是 Swift 的新手,我正在探索如果在使用范围迭代集合时修改集合会发生什么。我不明白为什么这两个循环有不同的行为: var things = ["a", "b", "c"] for i in 0.
我是一名优秀的程序员,十分优秀!