我有一个char-by-char函数,它返回一个文件中的字符。就我而言,这是非常高效的。
Xcode表示将在Swift 4.0中删除characterPointer.initialize(from: s.characters)
""我必须"使用' UnsafeMutableBufferPointer.initialize(来自:)'代替"
但我无法得到它。你能解释一下吗?如何使用带有UnsafeMutableBufferPointer的字符快速迭代器?你有一个例子吗?
这是我的功能:
/// Return next character, or nil on EOF.
func nextChar() -> Character? {
//precondition(fileHandle != nil, "Attempt to read from closed file")
if atEof {
return nil
}
if stored_cnt > (stored_idx + 1) {
stored_idx += 1
let char = characterPointer.pointee
characterPointer = characterPointer.successor()
return char
}
let tmpData = fileHandle.readData(ofLength: (chunkSize))
if tmpData.count == 0 {
atEof = true
return nil
}
if var s = NSString(data: tmpData, encoding: encoding.rawValue) as String! {
characterPointer.initialize(from: s.characters)
stored_idx = 0
stored_cnt = s.characters.count
}
let char = characterPointer.pointee
characterPointer = characterPointer.successor()
return char
}
顺便说一下,如果有更快的解决方案,请不要犹豫告诉我。
非常感谢。