我有一个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时,我收到一个错误:
Terminating app due to uncaught exception ‘NSRangeException’,reason:
‘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,我自己做了..
有线索吗?
解决方法 在Swift 3中,“集合移动他们的索引”,请参阅关于Swift进化的 A New Model for Collections and Indices.
在Swift 2.2中,advancedBy()和distanceto()方法是
调用索引:
let u16 = "12345".utf16let i1 = u16.startIndex.advancedBy(1)let i2 = u16.startIndex.advancedBy(3)let d = i1.distanceto(i2)print(d) // 2
这种方法仍然存在于Swift 3中,但至少在字符集上会产生意想不到的结果:
let u16 = "12345".utf16let 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".utf16let 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
适用于您的问题,这是您的方法
转换范围< 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,let from = String.Index(from16,within: self),let to = String.Index(to16,within: self) else { return nil } return from ..< to }}总结
以上是内存溢出为你收集整理的斯威夫特3;范围’越界’全部内容,希望文章能够帮你解决斯威夫特3;范围’越界’所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)