简体   繁体   English

如何比较Swift中的范围?

[英]How to compare ranges in Swift?

I'm not sure if it's a bug in XCode or I didn't understand ranges in Swift. 我不确定它是否是XCode中的错误,或者我不理解Swift中的范围。 Here is some code to show how range works: 以下是一些显示范围如何工作的代码:

let range = 0..<5
contains(range, 0) // true
contains(range, 5) // false
range.startIndex == 0 // true
range.endIndex == 5 // true

let str = "Hello, playground"
str.rangeOfString("Hello")! // 0..<5

Great! 大! Now let's use it real code: 现在让我们使用真正的代码:

let str = "Hello, playground"
if let range = str.rangeOfString("Hello") {
    if range.startIndex == 0 {
        print("str starts with 'Hello'")
    }
}

I'm getting following error in line that reads if range.startIndex == 0 { if range.startIndex == 0 {我在读取行中的跟踪错误

Cannot invoke '==' with an argument lis of type (String.index, IntegerLiteralConvertible)' 无法使用类型为(String.index,IntegerLiteralConvertible)的参数lis调用'=='

rangeOfString actually returns a range of String.Index s and not Int s; rangeOfString实际上返回一个String.Index的范围,而不是Int s; the two aren't comparable (even though the playground shows the range as 0..<5 ). 两者没有可比性(即使操场显示范围为0..<5 )。 You can read about the reasons for that in the answers to this question . 您可以在这个问题的答案中阅读有关原因的原因。

If you want your example to work then, instead of comparing range.startIndex with 0 , you should compare it with str.startIndex : 如果你想让你的例子工作,而不是将range.startIndex0进行比较,你应该将它与str.startIndex进行比较:

let str = "Hello, playground"
if let range = str.rangeOfString("Hello") {
    if range.startIndex == str.startIndex {
        print("str starts with 'Hello'")
    }
}

If you need to compare to the range's startIndex with, say, the second character index in the string, then you can use the advance function to increment str.startIndex by 1 and compare range.startIndex to that: 如果你需要比较的范围内的startIndex有,比方说,第二个字符的索引字符串中,那么你可以使用advance功能来增加str.startIndex1和比较range.startIndex到:

let str = "Hello, playground"
if let range = str.rangeOfString("e") {
    if range.startIndex == advance(str.startIndex, 1) {
        print("str's 2nd character is 'e'")
    }
}

If you need to know the length of the range, then you can use the distance function: 如果您需要知道范围的长度,那么您可以使用distance函数:

let str = "Hello, playground"
if let range = str.rangeOfString("play") {
    let length = distance(range.startIndex, range.endIndex)
    print("found a range \(length) characters long")
}

The ~= operator in Swift Swift中的〜=运算符

Sometimes, we have to check if a number is between a range, and as usual, we do something like : 有时,我们必须检查一个数字是否在一个范围之间,和往常一样,我们做的事情如下:

Check if number is between 0 and 100 include 检查数字是否在0到100之间

if number >=0 && number <= 100 {  
   // TODO:
}

It works, but we can do better and swiftier. 它有效,但我们可以做得更好,更快捷。 The Swift Standard library have an ~= operator, so we can do instead : Swift标准库有一个〜=运算符,所以我们可以这样做:

if 0...100 ~= number {  
   // TODO:
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM