为什么我的日志打印了这么多奇怪的字符并弄乱了函数的输出?
Why is my log printing so many weird characters and messing up the function's output?
我有一个功能,returns 在第三方键盘上输入的最后一个词:
var lastWordTyped: String? {
let proxy = self.textDocumentProxy as! UITextDocumentProxy
if let documentContext = proxy.documentContextBeforeInput as NSString? {
let length = documentContext.length
if length > 0 && NSCharacterSet.alphanumericCharacterSet().characterIsMember(documentContext.characterAtIndex(length - 1)) {
let components = documentContext.componentsSeparatedByCharactersInSet(NSCharacterSet.alphanumericCharacterSet().invertedSet) as! [String]
println(components)
return components[components.endIndex - 1]
}
}
return nil
}
由于某些原因,虽然函数运行正常并且returns输出正确,但我偶然查看了日志中的输出,发现它在对输出进行屠宰并多次输出(如中所见)下图)。
这会导致我的键盘内存使用量增加吗?它会减慢速度吗?
我该如何解决这个问题?
编辑 1:使用模拟器时无法重现此错误。
println
不是同步的,因此如果有多个线程调用您的代码,它们的输出可能会重叠。
我建议将 println(components)
替换为 NSLog(components)
,以便更好地了解实际情况。
我有一个功能,returns 在第三方键盘上输入的最后一个词:
var lastWordTyped: String? {
let proxy = self.textDocumentProxy as! UITextDocumentProxy
if let documentContext = proxy.documentContextBeforeInput as NSString? {
let length = documentContext.length
if length > 0 && NSCharacterSet.alphanumericCharacterSet().characterIsMember(documentContext.characterAtIndex(length - 1)) {
let components = documentContext.componentsSeparatedByCharactersInSet(NSCharacterSet.alphanumericCharacterSet().invertedSet) as! [String]
println(components)
return components[components.endIndex - 1]
}
}
return nil
}
由于某些原因,虽然函数运行正常并且returns输出正确,但我偶然查看了日志中的输出,发现它在对输出进行屠宰并多次输出(如中所见)下图)。
这会导致我的键盘内存使用量增加吗?它会减慢速度吗?
我该如何解决这个问题?
编辑 1:使用模拟器时无法重现此错误。
println
不是同步的,因此如果有多个线程调用您的代码,它们的输出可能会重叠。
我建议将 println(components)
替换为 NSLog(components)
,以便更好地了解实际情况。