斯威夫特3;范围'越界'

时间:2016-07-06 14:58:36

标签: swift string nsrange swift3

我刚刚将Xcode更新为8.0 beta 2和swift 3.0。从swift 2.3更新后,我遇到了很多错误。

我有一个String-extension,它将“self”-string中的Range转换为NSRange:

extension String {

    func NSRangeFromRange(_ range : Range<String.Index>) -> NSRange {

        let utf16view = self.utf16
        let from = String.UTF16View.Index(range.lowerBound, within: utf16view)
        let to = String.UTF16View.Index(range.upperBound, within: utf16view)

        print("to: \(to) from: \(from)")
        print(self.characters.count)

        return NSMakeRange(utf16view.startIndex.distance(to: from), from.distance(to: to))
    //    return NSMakeRange(0, 0)    // <-- removes exception
    }
}

执行NSMakeRange时出现错误:

  

由于未捕获的异常'NSRangeException'而终止应用程序,原因:   'NSMutableRLEArray objectAtIndex:effectiveRange :: Out of bounds'

当我打印到索引和索引时,我得到了:

  

to:Index(_offset:194)from:Index(_offset:181)

字符串的字符数为210,这似乎是正确的。

所以,我不明白为什么它告诉我索引超出界限,当它们少于总数时。

在我更新到swift 3之前,这条线路工作正常。当时看起来像这样:

return NSMakeRange(utf16view.startIndex.distanceTo(from), from.distanceTo(to))

自动转换器没有将语法从swift 2.3更新到3.0,我自己做了..

任何线索?

1 个答案:

答案 0 :(得分:7)

在Swift 3中,“集合移动他们的索引”,请参阅  关于斯威夫特进化的A New Model for Collections and Indices

在Swift 2.2中,advancedBy()distanceTo()方法是 呼吁索引:

let u16 = "12345".utf16
let i1 = u16.startIndex.advancedBy(1)
let i2 = u16.startIndex.advancedBy(3)
let d = i1.distanceTo(i2)
print(d) // 2

这种方法仍然存在于Swift 3中,但至少在字符集上会产生意想不到的结果:

let u16 = "12345".utf16
let i1 = u16.startIndex.advanced(by: 1)
let i2 = u16.startIndex.advanced(by: 3)
let d = i1.distance(to: i2)
print(d) // -2

正确的方法是使用index()distance()方法  集合本身:

let u16 = "12345".utf16
let i1 = u16.index(u16.startIndex, offsetBy: 1)
let i2 = u16.index(u16.startIndex, offsetBy: 3)
let d = u16.distance(from: i1, to: i2)
print(d) // 2

应用于您的问题,您就是这样做的 将Range<String.Index>转换为Swift 3中的相应NSRange (从https://stackoverflow.com/a/30404532/1187415复制):

extension String {
    func nsRange(from range: Range<String.Index>) -> NSRange {
        let utf16view = self.utf16
        let from = range.lowerBound.samePosition(in: utf16view)
        let to = range.upperBound.samePosition(in: utf16view)
        return NSMakeRange(utf16view.distance(from: utf16view.startIndex, to: from),
                              utf16view.distance(from: from, to: to))
    }
}

为了完整起见,这是相反的转换

extension String {
    func range(from nsRange: NSRange) -> Range<String.Index>? {
        guard
            let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex),
            let to16 = utf16.index(from16, offsetBy: nsRange.length, limitedBy: utf16.endIndex),
            let from = String.Index(from16, within: self),
            let to = String.Index(to16, within: self)
        else { return nil }
        return from ..< to
    }
}