现在该承认失败了…
在Objective-C中,我可以使用类似以下内容的东西:
NSString* str = @"abcdefghi"; [str rangeOfString:@"c"].location; // 2
在Swift中,我看到了类似的内容:
var str = "abcdefghi" str.rangeOfString("c").startIndex
…但是这给了我一个String.Index,可以用来将其下标回到原始字符串,但不能从中提取位置。
String.Index
FWIW,其中String.Index有一个私有ivar _position,其中具有正确的值。我只是看不到它的暴露方式。
_position
我知道自己可以轻松地将其添加到String中。我对这个新API中缺少的内容感到好奇。
您不是唯一找不到解决方案的人。
String没有实现RandomAccessIndexType。可能是因为它们启用了具有不同字节长度的字符。这就是为什么我们必须使用string.characters.count(count或countElements在Swift 1.x中)获取字符数的原因。这也适用于职位。的_position可能是一个索引字节的原始阵列,他们不希望公开这一点。本String.Index是为了保护我们从人物的中间访问字节。
String
RandomAccessIndexType
string.characters.count
count
countElements
这意味着您必须从String.startIndex或String.endIndex(String.Index实现BidirectionalIndexType)创建任何索引。可以使用successor或predecessor方法创建任何其他索引。
String.startIndex
String.endIndex
BidirectionalIndexType
successor
predecessor
现在可以使用索引来帮助我们,它提供了一组方法(Swift 1.x中的函数):
斯威夫特4.x
let text = "abc" let index2 = text.index(text.startIndex, offsetBy: 2) //will call succ 2 times let lastChar: Character = text[index2] //now we can index! let characterIndex2 = text.index(text.startIndex, offsetBy: 2) let lastChar2 = text[characterIndex2] //will do the same as above let range: Range<String.Index> = text.range(of: "b")! let index: Int = text.distance(from: text.startIndex, to: range.lowerBound)
斯威夫特3.0
let text = "abc" let index2 = text.index(text.startIndex, offsetBy: 2) //will call succ 2 times let lastChar: Character = text[index2] //now we can index! let characterIndex2 = text.characters.index(text.characters.startIndex, offsetBy: 2) let lastChar2 = text.characters[characterIndex2] //will do the same as above let range: Range<String.Index> = text.range(of: "b")! let index: Int = text.distance(from: text.startIndex, to: range.lowerBound)
斯威夫特2.x
let text = "abc" let index2 = text.startIndex.advancedBy(2) //will call succ 2 times let lastChar: Character = text[index2] //now we can index! let lastChar2 = text.characters[index2] //will do the same as above let range: Range<String.Index> = text.rangeOfString("b")! let index: Int = text.startIndex.distanceTo(range.startIndex) //will call successor/predecessor several times until the indices match
斯威夫特1.x
let text = "abc" let index2 = advance(text.startIndex, 2) //will call succ 2 times let lastChar: Character = text[index2] //now we can index! let range = text.rangeOfString("b") let index: Int = distance(text.startIndex, range.startIndex) //will call succ/pred several times
使用String.Index它很麻烦,但是使用包装器按整数索引很危险,因为它掩盖了实际索引的效率低下。
请注意,Swift索引实现存在一个问题,即为 一个字符串创建的索引/范围不能可靠地用于其他字符串 ,例如:
let text: String = "abc" let text2: String = "🎾🏇🏈" let range = text.rangeOfString("b")! //can randomly return a bad substring or throw an exception let substring: String = text2[range] //the correct solution let intIndex: Int = text.startIndex.distanceTo(range.startIndex) let startIndex2 = text2.startIndex.advancedBy(intIndex) let range2 = startIndex2...startIndex2 let substring: String = text2[range2]
let text: String = "abc" let text2: String = "🎾🏇🏈" let range = text.rangeOfString("b") //can randomly return nil or a bad substring let substring: String = text2[range] //the correct solution let intIndex: Int = distance(text.startIndex, range.startIndex) let startIndex2 = advance(text2.startIndex, intIndex) let range2 = startIndex2...startIndex2 let substring: String = text2[range2]