10.5 NSLog

In our previous post, we talked about how one of the main ways that we can access variable values is through debugging, and printing a variable's description in the console. There is another approach that is useful to know about, and it is called NSLog.

Unlike using the debugger, the real benefit of using NSLog is that you can print variable values to the console as the program executes. Let's take a look at a very simple example of how we use NSLog:


If you have been following along in my recent posts, you'll need to make sure that you are not in debug mode. Let's take a look at what we are doing, and try and make some sense of it. Our first log message is pretty straight forward. We are simply outputting a basic text message to the console. This could be useful to let us know where we are at in the sequence of code.

The next message is a little bit more complicated. We are outputting a basic message, but what is the deal with the %@ stuff? This is called a placeholder. A placeholder is used to represent a value that is on the way. So the compiler looks at the %@ sign and says 'Ok, I need to look for a string after the text message part of this NSLog is through'. Sure enough, if you look at where the text message ends, there is a , and then a variable. When the NSLog is actually executed, the compiler takes that variable's value and stuffs it into the text message exactly where the %@ was located. That's why it is called a placeholder.

The third and final NSLog is the same, but because we went off to another method and changed the variable value, the NSLog message is different.

The final point we need to discuss is the fact that there are multiple different placeholders for different types of variables. We used %@ because the NSString type is technically an object. There are lots of other types of objects (don't worry, we'll talk about them soon), and if you want to print them out in the NSLog, you need to use the %@ placeholder. Here are a few more placeholder's that you might find useful:

For printing out an integer: %d - (d? Don't ask me!)

For printing out a float/double (same thing) - %f

For printing out a boolean (technically they are just numbers, right? 1 or 0?) - %d

This is just a small sample of the different types of placeholders that you can use, for a more comprehensive list, you can Google around and find a nice list in no time, or look here.

NOTE: If you use the wrong placeholder for a variable when trying to print it's value, you will crash your app, and you will get a very vague and confusing error that will leave you scratching your head for hours. Make sure you are sure that you are using the correct placeholder!

To recap, NSLog is a very useful for printing variable values at run-time, or when the app is actually running. You will want to be careful not to use this tool too often, otherwise your console will become flooded with a large number of messages that it may be difficult to sort through.

Now, onto more about methods!


No comments:

Post a Comment