不建议使用“ init(start:end :)”:它将在Swift 3中删除。请使用“ .. <”运算符


72

我正在使用以下代码:

var continousDigitsRange:Range<Int> = Range<Int>(start: 0, end: 0)

自从更新到Xcode 7.3(Swift 2.2)以来,我得到了以下提示:

不建议使用“ init(start:end :)”:它将在Swift 3中删除。请使用“ .. <”运算符。

对我来说,不清楚如何使用“ .. <”运算符正确地对其进行“翻译”。


for 0 in .. <item {} for 0 ... 100 {}
Anton

Answers:


88

你应该简单地写

var continousDigitsRange1:Range<Int> = 0..<0

或者如果您想更简单

var continousDigitsRange = 0..<0

35

同样值得注意的是,对于substringWithRange字符串,您现在可以使用

let theString = "Hello, how are you"
let range = theString.startIndex.advancedBy(start) ..< theString.startIndex.advancedBy(end)
theString.substringWithRange(range)

1
一件事是,如果您从字符串的末尾开始计数,请使用endIndex而不是像下面这样开始:'let range = theString.startIndex.advancedBy(start).. <theString.endIndex.advancedBy(end)'
Hamid

8

封闭范围运算符(a...b)定义一个从a到b的范围,并包括值a和b。a的值不能大于b。

半开范围运算符(a..<b)定义从a到b的范围,但不包括b。据说它是半开的,因为它包含其第一个值,但不包含其最终值。与封闭范围运算符一样,a的值不得大于b。如果a的值等于b,则结果范围将为空。

Swift编程语言(Swift 2.2)-基本运算符

var continousDigitsRange:Range<Int> = Range<Int>(start: 0, end: 0)
--to--
var continousDigitsRange:Range<Int> = 0..<0

1
嗯,是的,很好,将其切换了-想要快速说明范围选项。
电磁线圈

什么以ContinuousDigitsRange结尾?对我来说似乎是零。此范围可用于什么用途?我也在努力学习!
user462990 '16

2

完整显示bmichotte的答案...

let theString = "Hello, how are you today my friend"
    let start = 3
    let end = 15
    let range = theString.startIndex.advancedBy(start) ..< theString.startIndex.advancedBy(end)
    let p = theString.substringWithRange(range)
    print("this is the middle bit>\(p)<")

这产生这是中间位> lo,<


1

参考swift 3.0添加一些要点

//可数范围示例。

let range1 = 0..<5

可数近距离范围示例

let range2 = 0...5

//超出范围

let range = Range(uncheckedBounds: (range1.lowerBound,range1.upperBound))

//获取与substringRange的距离。

let str = "Hello, how are you"
let substringRange = str.characters.indices

//在Swift 3.0以下

let length = substringRange.distance(from: substringRange.startIndex, to: substringRange.endIndex)

//对于Swift 3.0

let length2 = str.distance(from: substringRange.startIndex, to: substringRange.endIndex)

1

我一直有一个函数来获取字符串的子字符串范围。这是我为Swift 3更新的功能:

func getSubStringRange(fullString: String, fromIndex: Int, subStringSize: Int) -> Range<String.Index> {
    let startIndex = fullString.characters.index(fullString.startIndex, offsetBy: fromIndex)
    let endIndex = fullString.characters.index(startIndex, offsetBy: subStringSize)

    let subStringRange = startIndex..<endIndex

    return subStringRange
}

该函数很容易说明-您传入一个字符串(fullString),子字符串开始处的字符串索引(fromIndex)和subString的大小(subStringSize)。

例:

let greeting = "Hi, my name is Nathaniel"
let getName = greeting[getSubStringRange(fullString: greeting, fromIndex: 15, subStringSize: 9)]

print("Name: \(getName)")

->印刷品:“名称:纳撒尼尔”


1
无法使用Swift 3进行编译。)之后没有关闭subStringSize: Int
Vladislav

1
endIndexfullString.之前错过的酒店startIndex,因此正确的方法是:let endIndex = fullString.characters.index(fullString.startIndex, offsetBy: subStringSize)
Mette

多谢你们。键入时我一定已经睡着了!发布时,我不得不手动输入全部代码,而不是像在Windows PC上那样直接从代码中直接复制/粘贴!
Krivvenz
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.