38

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#.

3 Answers 3

57
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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Just after I posted this I typed NS in the editor and saw NSLog come up. Tried it and go figure, it does what it says, logs! :-)
11

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);

Comments

7

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

1 Comment

Upvoted as this also avoids the need to stop, recompile, restart as you add 'output'. Also filtering the console to just show 'Debugger Output' filters out other unwanted 'output'. Brilliant. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.