Swift 3中Range对象上的NSRange.location的等效值是多少?

mat*_*ino 8 range nsrange swift3

有人可以给我一个提示,指出Range的哪个属性是NSRange的location属性的等价属性.

特别是我对如何从Swift 2.3 - > Swift 3.0迁移以下代码行感兴趣

if myRange.location != NSNotFound { ... }
Run Code Online (Sandbox Code Playgroud)

myRange仍然是Range属性,因此编译器告诉我正确: Type Range的值没有成员位置

检查空房子是否足够?

if !myRange.isEmpty { ... }
Run Code Online (Sandbox Code Playgroud)

提前致谢

ody*_*yth 3

就像评论所说,您将得到一个 nil 范围,而不是返回 NSNotFound 。

为了回答您的问题,.location 已替换为 .lowerBound 和 .upperBound。

let s = "The yellow dog is nice"
if let range = s.range(of: "dog")
{
    print(s[range.lowerBound...]) //prints "dog is nice"
    print(s[range.upperBound...]) //prints " is nice"
    print(s[range.lowerBound..<range.upperBound]) //prints "dog"
}
Run Code Online (Sandbox Code Playgroud)