斯威夫特 3;范围“越界" [英] Swift 3; Range 'Out of bounds'

查看:24
本文介绍了斯威夫特 3;范围“越界"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've just updated Xcode to 8.0 beta 2 and swift 3.0. After updating from swift 2.3 i'm getting a lot of errors.

我有一个字符串扩展,它将self"字符串中的范围转换为 NSRange:

I have a String-extension that's converting a Range in the "self"-string to a 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 时出现错误:

When NSMakeRange is executed I'm getting a error:

由于未捕获的异常NSRangeException"而终止应用,原因:'NSMutableRLEArray objectAtIndex:effectiveRange:: 越界'

Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'

当我打印 to- 和 from-index 时,我得到:

When I'm printing the to- and from-index's, I'm getting:

到:索引(_offset:194)来自:索引(_offset:181)

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

String 的字符数是 210,这似乎是正确的.

The character-count of the String is 210, which seems about right.

所以,我不明白为什么它告诉我索引超出范围,当它们小于总数时.

在我更新到 swift 3 之前,这条线运行良好.当时它看起来像这样:

This line was working perfectly before I updated to swift 3. Back then it was looking like this:

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

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

The auto-converter didn't update the syntax from swift 2.3 to 3.0, I did that myselves..

有什么线索吗?

推荐答案

在 Swift 3 中,Collections move their index",参见集合和索引的新模型 关于 Swift 进化.

In Swift 3, "Collections move their index", see A New Model for Collections and Indices on Swift evolution.

在 Swift 2.2 中,advancedBy()distanceTo() 方法是在索引上调用:

In Swift 2.2, the advancedBy() and distanceTo() methods are called on the index:

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 中仍然存在,但会产生意想不到的结果,至少在字符集合上:

This methods still exist in Swift 3 but give unexpected results, at least on character collections:

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()方法集合本身:

The correct way is to use the index() and distance() methods of the collection itself:

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

应用于您的问题,这就是您的方法在 Swift 3 中将 Range 转换为相应的 NSRange(复制自 https://stackoverflow.com/a/30404532/1187415):

Applied to your problem, this is how you can convert a Range<String.Index> to the corresponding NSRange in Swift 3 (copied from 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))
    }
}

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

And for the sake of completeness, this is the opposite conversion

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
    }
}

这篇关于斯威夫特 3;范围“越界"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆