gpt4 book ai didi

xcode10 - 打印大量文本时,Xcode 10 控制台日志很慢

转载 作者:行者123 更新时间:2023-12-04 13:02:30 29 4
gpt4 key购买 nike

我注意到在 Xcode 10 中打印大量文本时控制台日志的性能显着下降。例如,在打印收集服务器数据时就会发生这种情况。是否有任何已知的解决方案或信息?

最佳答案

我在 Swift 中遇到了同样的问题 print命令,但前提是附加了调试器。

我通过检查是否连接了调试器然后使用 NSLog 解决了这个问题。而不是 print .

要检查您的应用程序是否在调试器模式下运行,请参见此处:Detect if Swift app is being run from Xcode

考虑到 Xcode 8 + iOS 10 及更高版本的日志条目被截断,请参见此处:NSLog on devices in iOS 10 / Xcode 8 seems to truncate? Why?

2019 年 3 月 5 日编辑:

我使用来自 https://stackoverflow.com/a/39907046/6330248 的解决方案修复了它

swift 4.2:

extension String {
func components(withLength length: Int) -> [String] {
guard length > 0 else {
return [self]
}

return stride(from: 0, to: self.count, by: length).map {
let start = self.index(self.startIndex, offsetBy: $0)
let end = self.index(start, offsetBy: length, limitedBy: self.endIndex) ?? self.endIndex
return String(self[start..<end])
}
}
}

func amIBeingDebugged() -> Bool {
var info = kinfo_proc()
var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
var size = MemoryLayout<kinfo_proc>.stride
let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
assert(junk == 0, "sysctl failed")
return (info.kp_proc.p_flag & P_TRACED) != 0
}

func FixedNSLog(_ format: String, _ args: CVarArg...) {

let maxStringLength = 800
let string = String(format: format, arguments: args)

let substrings = string.components(withLength: maxStringLength)
let substringsCount = substrings.count

if substringsCount > 1 {
NSLog("!!! WARNING !!! The following message is printed by \(substringsCount) parts, because of Xcode 10 console issues")
}

substrings.forEach { (substring) in
NSLog(substring)
}

}

func log(_ message: String) {
if amIBeingDebugged() {
FixedNSLog(message)
}
else {
print(message)
}
}

关于xcode10 - 打印大量文本时,Xcode 10 控制台日志很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52411112/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com