从 swift 3.0 中删除语句的 C 样式,successor() 不可用
C style for statement removed from swift 3.0, successor() is unavailable
谁能帮我把这个 for 循环更新到 swift 3.0。帮助赞赏。谢谢!
for var index = trimmedString.startIndex; index < trimmedString.endIndex; index = index.successor().successor() {
let byteString = trimmedString.substringWithRange(Range<String.Index>(start: index, end: index.successor().successor()))
let num = UInt8(byteString.withCString { strtoul([=11=], nil, 16) })
data?.appendBytes([num] as [UInt8], length: 1)
}
在Swift3中,"Collections move their index",见
A New Model for Collections and Indices 关于 Swift 进化。特别是,
let toIndex = string.index(fromIndex, offsetBy: 2, limitedBy: string.endIndex)
将索引 fromIndex
提高 2 个字符位置,但仅
如果它符合索引的有效范围,并且 returns nil
除此以外。因此循环可以写成
let string = "0123456789abcdef"
let data = NSMutableData()
var fromIndex = string.startIndex
while let toIndex = string.index(fromIndex, offsetBy: 2, limitedBy: string.endIndex) {
// Extract hex code at position fromIndex ..< toIndex:
let byteString = string.substring(with: fromIndex..<toIndex)
var num = UInt8(byteString.withCString { strtoul([=11=], nil, 16) })
data.append(&num, length: 1)
// Advance to next position:
fromIndex = toIndex
}
print(data) // <01234567 89abcdef>
谁能帮我把这个 for 循环更新到 swift 3.0。帮助赞赏。谢谢!
for var index = trimmedString.startIndex; index < trimmedString.endIndex; index = index.successor().successor() {
let byteString = trimmedString.substringWithRange(Range<String.Index>(start: index, end: index.successor().successor()))
let num = UInt8(byteString.withCString { strtoul([=11=], nil, 16) })
data?.appendBytes([num] as [UInt8], length: 1)
}
在Swift3中,"Collections move their index",见 A New Model for Collections and Indices 关于 Swift 进化。特别是,
let toIndex = string.index(fromIndex, offsetBy: 2, limitedBy: string.endIndex)
将索引 fromIndex
提高 2 个字符位置,但仅
如果它符合索引的有效范围,并且 returns nil
除此以外。因此循环可以写成
let string = "0123456789abcdef"
let data = NSMutableData()
var fromIndex = string.startIndex
while let toIndex = string.index(fromIndex, offsetBy: 2, limitedBy: string.endIndex) {
// Extract hex code at position fromIndex ..< toIndex:
let byteString = string.substring(with: fromIndex..<toIndex)
var num = UInt8(byteString.withCString { strtoul([=11=], nil, 16) })
data.append(&num, length: 1)
// Advance to next position:
fromIndex = toIndex
}
print(data) // <01234567 89abcdef>