菜鸟教程小白 发表于 2022-12-12 09:08:47

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


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

<p>如何将 xcode 输出保存到文件中?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>可能不是你想要的,但我用这个:</p>

<pre><code>- (void)logIt:(NSString *)string {
    // First send the string to NSLog
    NSLog(@&#34;%@&#34;, string);

    // Setup date stuff
    NSDateFormatter *formatter = [ init];
    ;
    NSDate *date = ;

    // Paths - We&#39;re saving the data based on the day.
    NSString *path = ];
    NSString *writePath = [ stringByAppendingPathComponent:path];

    // We&#39;re going to want to append new data, so get the previous data.
    NSString *fileContents = ;

    // Write it to the string
    string = , string];

    // Write to file stored at: &#34;~/Library/Application\ Support/iPhone\ Simulator/*version*/Applications/*appGUID*/Documents/*date*-logFile.txt&#34;
    ;
}
</code></pre>

<p>这会将数据写入您设备上的文件(每日文件)。如果您希望在每次 session 后重置它,您当然可以修改代码来做到这一点。</p>

<p>当然,您必须将所有现有的 <code>NSLog()</code> 调用更改为使用 <code></code>。</p>

<p>这也适用于真实设备(但文件位置当然不同)。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 将 NSLog 保存到本地文件中,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22871855/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22871855/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 将 NSLog 保存到本地文件中