• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 将 NSLog 保存到本地文件中

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:08:47 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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



Best Answer-推荐答案


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

- (void)logItNSString *)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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap