Tyrel Van Niekerk
Tyrel Van Niekerk

Reputation: 1712

Write debug messages to Xcode output window

I have see this in sample objective c code before, but can't find it now and all the searches come back with irrelivent results.

I want to write debug messages to the Xcode output window. What's the command to do that? Basically like System.Diagnostics.Debug.WriteLine for C#.

Upvotes: 38

Views: 44622

Answers (3)

Will Pragnell
Will Pragnell

Reputation: 3133

NSLog(@"Your message here");

...should do it.

To include data from variables you can use string formatting, e.g:

NSLog(@"Value of string is %@", myNSString);

There are a bunch of different string format specifiers, you can look at them here: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

Upvotes: 57

StevenHolland
StevenHolland

Reputation: 106

It's better to write the debug messages using debugger breakpoints instead of cluttering your code with NSLog messages. Breakpoints will also save you from having to remove all those log messages when you ship your app.

To do this, set a breakpoint in Xcode, double-click on it, and click the Add Action button in the pop-up window. Select "Log Message" and type your message. Check the "Automatically continue after evaluating" check box at the bottom to keep it from pausing execution on the breakpoint

Upvotes: 7

sch
sch

Reputation: 27536

You are looking for NSLog. Calling

NSLog(@"Message");

will print Message on the console.

See here for more info about how to use string formatters to print the values of variables like in the examples below:

NSLog(@"This is a string: @", aString);
NSLog(@"This is an int: %d", anInt);

Upvotes: 11

Related Questions