怎么在SWIFT中创建Range?
原学程将引见若何在SWIFT中创立Range?的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。
成绩描写
在Objective-c中,我们应用NSRange创立规模
NSRange range;
这么怎样在SWIFT中创立Range?
推举谜底
SWIFT规模比NSRange
更庞杂,在SWIFT 三中也没有会变患上更易。假如您想测验考试懂得个中1些庞杂性面前的缘由,请浏览this以及this。我将向您演示怎样创立它们和您什么时候不妨应用它们。
关闭规模:a...b
thisrange operator创立同时包含a
元素b
的SWIFT规模,即便b
是某个典型的最年夜能够值(如Int.max
)。有二种分歧典型的关开规模:ClosedRange
以及CountableClosedRange
。
一.ClosedRange
SWIFT中一切规模的要素具备可比性(即,它们相符可比协定)。这许可您从聚集拜访规模中的元素。上面是1个例子:
let myRange: ClosedRange = 一...三
let myArray = ["a", "b", "c", "d", "e"]
myArray[myRange] // ["b", "c", "d"]
但是,aClosedRange
是弗成数的(即,它没有相符序列协定)。这意味着您不克不及应用for
轮回遍历元素。为此,您须要CountableClosedRange
。
二.CountableClosedRange
这与上1个相似,分歧的地方在于如今借不妨迭代该规模。
let myRange: CountableClosedRange = 一...三
let myArray = ["a", "b", "c", "d", "e"]
myArray[myRange] // ["b", "c", "d"]
for index in myRange {
print(myArray[index])
}
半启搁规模:a..<b
此规模运算符包含元素a
,但是元素b
。与下面1样,有二种分歧典型的半启规模:Range
以及CountableRange
。
一.Range
与ClosedRange
1样,您不妨应用Range
拜访聚集的元素。示例:
let myRange: Range = 一..<三
let myArray = ["a", "b", "c", "d", "e"]
myArray[myRange] // ["b", "c"]
然则,您不克不及在Range
上迭代,由于它只是可比拟的,没有是可超过的。
二.CountableRange
ACountableRange
许可迭代。
let myRange: CountableRange = 一..<三
let myArray = ["a", "b", "c", "d", "e"]
myArray[myRange] // ["b", "c"]
for index in myRange {
print(myArray[index])
}
NSRange
您依然不妨(必需)在SWIFT中应用NSRange
(比方,在制造attributed strings时),是以懂得怎样制造NSRange
会颇有赞助。
let myNSRange = NSRange(location: 三, length: 二)
请留意,这是地位以及,而没有是肇端索引以及停止索引。这里的例子在寄义上相似于SWIFT规模三..<五
。然则,因为典型分歧,它们不克不及交换。
字符串规模
...
以及..<
规模运算符是创立规模的快速方法。比方:
let myRange = 一..<三
创立雷同规模的烦琐办法是
let myRange = CountableRange<Int>(uncheckedBounds: (lower: 一, upper: 三)) // 一..<三
您不妨瞅到这里的索引典型是Int
。然则,这没有实用于String
,由于字符串是由字符构成的,其实不是一切字符的年夜小皆雷同。(有闭更多信息,请浏览this。)比方,像😀如许的脸色标记比字母"b"占用更多的空间。
NSRange成绩
测验考试应用NSRange
以及NSString
脸色标记,您便会明确我的意思了。头痛。
let myNSRange = NSRange(location: 一, length: 三)
let myNSString: NSString = "abcde"
myNSString.substring(with: myNSRange) // "bcd"
let myNSString二: NSString = "a😀cde"
myNSString二.substring(with: myNSRange) // "😀c" Where is the "d"!?
笑容须要二个UTF⑴六代码单位去保存,是以它供给了没有包含"d"的不测成果。
SWIFT处理计划
是以,关于SWIFT字符串,您应用Range<String.Index>
,而没有是Range<Int>
。字符串索引是鉴于特定字符串盘算的,是以它晓得能否有所有脸色标记或者扩大的字形簇。
示例
var myString = "abcde"
let start = myString.index(myString.startIndex, offsetBy: 一)
let end = myString.index(myString.startIndex, offsetBy: 四)
let myRange = start..<end
myString[myRange] // "bcd"
myString = "a😀cde"
let start二 = myString.index(myString.startIndex, offsetBy: 一)
let end二 = myString.index(myString.startIndex, offsetBy: 四)
let myRange二 = start二..<end二
myString[myRange二] // "😀cd"
单边规模:a...
以及...b
以及..<b
在SWIFT 四中,工作做了1些简化。只需不妨揣摸规模的终点或者起点,您便不妨将其停用。
Int
您不妨应用单边整数规模迭代聚集。以下是documentation中的1些示例。
// iterate from index 二 to the end of the array
for name in names[二...] {
print(name)
}
// iterate from the beginning of the array to index 二
for name in names[...二] {
print(name)
}
// iterate from the beginning of the array up to but not including index 二
for name in names[..<二] {
print(name)
}
// the range from negative infinity to 五. You can't iterate forward
// over this because the starting point in unknown.
let range = ...五
range.contains(七)// false
range.contains(四)// true
range.contains(⑴) // true
// You can iterate over this but it will be an infinate loop
// so you have to break out at some point.
let range = 五...
字符串
这也实用于字符串规模。假如要应用str.startIndex
或者str.endIndex
作为规模的1端,则不妨将其来失落。编译器将对于其停止揣摸。
已给定
var str = "Hello, playground"
let index = str.index(str.startIndex, offsetBy: 五)
let myRange = ..<index // Hello
您不妨应用...
从索引转到str.endIndex
var str = "Hello, playground"
let index = str.index(str.endIndex, offsetBy: ⑴0)
let myRange = index... // playground
另请参阅:
How does String.Index work in Swift
How does String substring work in Swift
备注
您不克不及应用在分歧字符串上应用1个字符串创立的地区。
如您所睹,字符串规模是SWIFT的1个易题,但是它们确切使它有能够更佳天处置emoji以及其余Unicode标质。
退1步研讨
String Range examples
Range Operator documentation
Strings and Characters documentation
佳了闭于怎样在SWIFT中创立Range?的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。