NSExplorer
NSExplorer

Reputation: 12149

How to clear console programmatically in Xcode?

I have a bunch of NSLog statements in my code which I use for debugging. Every time I run my Project I'd like to start from a fresh console screen. Is there any command I can embed in my code which can do this?

Upvotes: 36

Views: 25601

Answers (8)

eharo2
eharo2

Reputation: 2642

I went through all the answers and there are some alternatives but I think that the final answer is: You can not (as of Jun 2021).

The only alternative I use is: Add a breakpoint. When the execution hits the breakpoint, click Ctr-K. Resume execution.

Upvotes: 2

Ashwani
Ashwani

Reputation: 1584

When in the console (Debug mode), use:

Xcode > Debug > Debug Workflow > Clear Console

Keyboard Shortcut: Command ⌘+K

Upvotes: 86

stackr
stackr

Reputation: 2742

Maybe you could use the "Auto Clear Debug Console" setting in the Xcode Preferences...

Don't know if this answers your question?

Upvotes: 6

Tertium
Tertium

Reputation: 247

Swift

print("any value", terminator: Array(repeating: "\n", count: 100).joined())

Objective-C

I think the only thing you can is

for(int i= 0; i < 100; i++) 
NSLog(@" ");

just like in good old MS-DOS :)

Upvotes: 23

Aidan
Aidan

Reputation: 5536

As mentioned by stackr, the "Auto Clear Debug Console" setting in the XCode Preferences will do this. To do it in code:

bool ClearXCodeDebuggerConsole()
{
    NSString *const scriptText = @"\
tell application \"System Events\"\n\
set currentapp to the name of the current application\n\
end tell\n\
tell application \"Xcode\" to activate\n\
tell application \"System Events\"\n\
keystroke \"r\" using {command down, control down, option down}\n\
end tell\n\
tell application currentapp to activate\n\
return true";

    NSAppleScript *script = [[[NSAppleScript alloc] initWithSource:scriptText] autorelease];
    [scriptText release];
    NSDictionary *dictError = nil;
    NSAppleEventDescriptor *result = [script executeAndReturnError:&dictError];

    if (!result) return false;
    if ([result booleanValue] != YES) return false;
    return true;
}

Upvotes: 1

Dinesh619
Dinesh619

Reputation: 61

command + control + options + R clears the console in xcode

Upvotes: -5

Joshua Nozzi
Joshua Nozzi

Reputation: 61228

The debugger console / run log are basically a redirected "log this to the console" command from your app. "Clearing" it means nothing in the general sense, since the messages are usually just shunted somewhere (like a file). Your application would have to know about its debugging environment and be able to tell that environment to clear whatever it's logging to.

In short: I suppose it's not impossible but it's ridiculously inconvenient.

Upvotes: 3

Mark
Mark

Reputation: 6128

If you are talking about the console in the Xcode window there is a "Clear Console" option in the "Run" menu. There is also, in the "Debugging" Preferences tab an "Auto Clear Debug Console" checkbox. I am referring Xcode 3.2.x

Upvotes: 10

Related Questions