Xcode,为什么一些NSLog条目从控制台中省略? [英] Xcode, Why are some NSLog entries omitted from the console?

查看:157
本文介绍了Xcode,为什么一些NSLog条目从控制台中省略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习IOS开发。每次我运行这个程序,控制台并不总是显示我的NSLogs。

I'm just beginning to learn IOS development. Everytime I run this program, the console does not always display my NSLogs.

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSMutableArray *items = [[NSMutableArray alloc] init];

        [items addObject:@"One"];
        [items addObject:@"Two"];
        [items addObject:@"Three"];

        [items insertObject:@"zero" atIndex:0];

        NSLog(@"Object at 0 is %@", [items objectAtIndex:0]);
        NSLog(@"Object at 1 is %@", [items objectAtIndex:1]);
        NSLog(@"Object at 2 is %@", [items objectAtIndex:2]);
        NSLog(@"Object at 3 is %@", [items objectAtIndex:3]);

        for (int i = 0; i < [items count]; i++) {
            NSLog(@"%@", [items objectAtIndex:i]);
        }
        items = nil;           
    }
    return 0;
}



我应该在控制台中每次运行它: p>

I should be getting this in the console every time I run it:

2012-04-11 16:29:10.419 RandomPossessions[701:403] Object at 0 is zero
2012-04-11 16:29:10.421 RandomPossessions[701:403] Object at 1 is One
2012-04-11 16:29:10.422 RandomPossessions[701:403] Object at 2 is Two
2012-04-11 16:29:10.422 RandomPossessions[701:403] Object at 3 is Three
2012-04-11 16:29:10.423 RandomPossessions[701:403] zero
2012-04-11 16:29:10.423 RandomPossessions[701:403] One
2012-04-11 16:29:10.424 RandomPossessions[701:403] Two
2012-04-11 16:29:10.424 RandomPossessions[701:403] Three

但有时不是全部显示。另一次我运行它,我可以得到:

But sometimes it's not all displayed. Another time I run it and I could just get:

2012-04-11 16:48:20.626 RandomPossessions[734:403] Object at 0 is zero
2012-04-11 16:48:20.628 RandomPossessions[734:403] Object at 1 is One

我必须运行程序几次才能得到NSLogs的完整列表输出到控制台。这是一个错误,还是我做错了什么?
感谢

I have to run the program several times to get the full list of NSLogs to output to the console. Is this a bug, or am I doing something wrong? Thanks

推荐答案

令人着迷。我只是尝试你的代码,得到相同的结果。这是我的假设:

Fascinating. I just tried your code and got the same result. Here's my hypothesis:

在您的行设置断点:

    [items insertObject:@"zero" atIndex:0];

当调试器到达那里时,调试面板显示一个线程。

When the debugger gets there, the debug panel shows one thread.

现在转到

    NSLog(@"Object at 1 is %@", [items objectAtIndex:1]);

现在有3个主题。

我打赌那些其他2线程正在做实际写入日志。如果程序在这些单独的线程写入日志之前完成,那么它们只会被杀掉,并且您的日志不会被写入。

I'm betting that those others 2 threads are doing the actual writing to the log. If the program finishes before those separate threads have written to the log, then they just get killed, and your logs don't get written.

我尝试添加一个调用

sleep(0);

,突然日志开始正常显示。

at the end of the main function, and suddenly the logs started appearing appropriately.

然后,我尝试删除该行,原来的症状消失了,我无法再重现您遇到的问题。

I then tried removing that line, and the original symptoms disappeared, and I can no longer recreate the issue you were having.

底线:我认为这是在日志写入之前程序退出的症状。如果你在写一个iOS应用程序,你不必担心这种事情。这只是一个晦涩的实施细节。

Bottom line: I think this is a symptom of the program quitting before the logs have been written. If you're writing an iOS app, you won't have to worry about this kind of thing. It's just an obscure implementation detail.

祝你好运。

这篇关于Xcode,为什么一些NSLog条目从控制台中省略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆