gpt4 book ai didi

ios - 将 NSLog 保存到本地文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:36 25 4
gpt4 key购买 nike

我正在学习如何为 iPhone 开发,我需要将 de NSLog 输出保存在我机器上的本地文件中以分析结果,因为我会长时间运行该应用程序并且我需要在一些之后进行检查运行几个小时输出什么(我想不时将输出文件保存在我的机器上,例如每 30 分钟后)。

如何将 xcode 输出保存到文件中?

最佳答案

可能不是你想要的,但我用这个:

- (void)logIt:(NSString *)string {
// First send the string to NSLog
NSLog(@"%@", string);

// Setup date stuff
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-dd-MM"];
NSDate *date = [NSDate date];

// Paths - We're saving the data based on the day.
NSString *path = [NSString stringWithFormat:@"%@-logFile.txt", [formatter stringFromDate:date]];
NSString *writePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:path];

// We're going to want to append new data, so get the previous data.
NSString *fileContents = [NSString stringWithContentsOfFile:writePath encoding:NSUTF8StringEncoding error:nil];

// Write it to the string
string = [NSString stringWithFormat:@"%@\n%@ - %@", fileContents, [formatter stringFromDate:date], string];

// Write to file stored at: "~/Library/Application\ Support/iPhone\ Simulator/*version*/Applications/*appGUID*/Documents/*date*-logFile.txt"
[string writeToFile:writePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

这会将数据写入您设备上的文件(每日文件)。如果您希望它在每次 session 后重置,您当然可以修改代码来执行此操作。

当然,您必须更改所有现有的 NSLog() 调用以使用 [self logIt:]

这也适用于真实设备(当然文件位置不同)。

关于ios - 将 NSLog 保存到本地文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22871855/

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