{%Pragmatic Coding%} Tips and tricks for software developers.

22Jan/121

NSLog – from beginning to advanced.

Hello friends!
First of all thanks to my friends were asking me about NSLog features, so, I decided to write about it.
So, let's get start with introduction where I will describe what is NSLog and basic usage of it.

NSLog introduction and basic usage

NSLog () - the most simplest way to display information on the screen. Originally this feature was designed to display errors. But because of the ease of use, most programmers use it to display some data while testing programs.

NSLog is a FoundationKit function for printing debug statements to the console. It is defined in NSObjCRuntime.h:

NSLog works basically like:

But its format string is a NSString object rather than simple char *, and a timestamp is prepended to the output. You can use it in the predictable ways:

The following example uses the function NSLog

As a result of this code, "Starting our example!" text will be displayed. The text between the @ "and" is called a string. Except for the line function NSLog () displays a variety of additional information, current date and name of the application.

Actually the string may have zero length or more characters. NSLog (@""); this expression contains 0 characters, it is an empty string (for example, the string whose length is equal to 0). Expression NSLog (@" "); is not an empty string, even though it looks very similar. It contains a gap, and in this case length is equal to 1.

Using NSLog(); we can use escape-characters to format our output text. For the function NSLog () they are a signal that the next character is not an ordinary letter and character that has special meaning.
For example:

Will display

In our example, n represents the "start a new line." But how to be in a situation where you want to display the quote character or backslash? It's enough to put another backslash before the character that we want to bring:

Will display

Dynamic output

Till this moment we had been show only a static string. Let's print the calculated value.

Notice that in brackets we have regular formatted string, the comma and the variable name. The string contains of string formatter %d, that means the input parameter is an small decimal number value type. More detail about string formatting I will describe further in this article.
The result for the sample will be:

By the way, you will ask how can I make an output for simple string param? Here is example where we are using %@ formatter for string value:

To print the Real values ​​you should use% f instead of % d. For example:

To indicate the accuracy of the numbers you should write %.2f that will format float value with 2 digits after dot:

Logically, the function NSLog () can display more than one value, or any combination of values. The main thing here is not mess with an indication of the type of data.

NSLog format specifiers

NSLog. Getting Advanced

In this section I'd like to review some macros examples that could be useful for you me dear reader!
Here is a good macros I found in stakowerflow website.

The DLog macro is used to only output when the DEBUG variable is set (-DDEBUG in the projects's C flags for the debug configuration).
ALog will always output text (like the regular NSLog).

The output (e.g. ALog(@"Hello world") ) will look like this:

But for me it's not enough debug information and I'd like to extend this functionality to be be able to display file source.
F.e. I'd like to show file full path, such as

In this case I will write a function that will display NSLog output as I want.

This is a simple function you can use in your application to format NSLog output. For better use create a singelton class not to mess in your code.
And here is example declaration in header file for comfortable use of this function.

Here is demonstration of usage of this snippet in function:

Looks great!

And, at last, few days ago my friend asked me how to make an output in the log line of certain length ... or better to say so ... we have lines of varying length, but it's need to be that they were all in the same length in output.
For example we have a strings: "one" "two" "one hundred fifty-five" and on the output should be something like this:

In other words, all the figures were one above the other.
Well in the first look it's not hard, you could suppose to use tabs "\t" in NSLog output. Not really. Well thanks DarkAn he finally found a solution.
In this case we need to use NSString formatting. NSString has a method stringByPaddingToLength:withString:startingAtIndex: that returns a new string formed from the receiver by either removing characters from the end, or by appending as many occurrences as necessary of a given pad string.
So, here is an example of using such method:

For more details about using stringByPaddingToLength:withString:startingAtIndex: function please read the official SDK documentation.

Than't it! That's all came to my mind at this moment about NSLog.
Hope this article were helpful to you my dear reader!
Any suggestions about using NSLog are welcome in the comments.
Thanks!

Share on social network

Share to Facebook
Share to Google Plus
Share to LiveJournal
Share to MyWorld
Share to Odnoklassniki
Share to Yandex
Comments (1) Trackbacks (0)
  1. Nice, very cool article


Leave a comment

No trackbacks yet.