Okay. I get it. You're bored with hello world. I'm bored with hello world. So, let's get into this language. We'll start with variables and calculations that will lead us into loops, and conditions, and data types. In the next few movies we're going to cover the core language syntax. Much of which, of course, is based on C. So if you've spent the last ten years living in C and C++, you are likely to find some of this remedial. Yes, I get it. You know this already. Objective-C did not try and invent its own way to write an if statement. So you can jump ahead, or I invite you to sit back and sing along.
So, let me go ahead and do that. So, I will create a few variables, do our first calculation and then our first slightly interesting output message. But probably raising a few questions if you're new to this language, like what exactly is this at sign after NS log and what does this per cent I mean on line 22. So let's take this one by one. Variables in Objective-C are declared as in other C-based languages. First you give it a type, and then you give it a name, then a semicolon.
Optionally, you can give it a value at the same time that you declare it. Now, in Objective-C, we type our variables. If you're coming from a language where the same variable can first be an integer and then change its value to a float and then to a string and then to an object, that''s not the case here. This variable, highScore, can only hold an integer value. It's defined as an int. So you pick your type when you define your variable. Way down the line there is something in an Objective-C called dynamically typing. But that is for much more complex situations and it is not what we do here.
So in this code we are beginning here with four integer variables. Whole numbers with nothing after the decimal point. Minutes equals 60. Hours is 24. Days is 365 and then I create a new variable called minutes in a year and then I set it to the calculation of multiplying all those by each other. As in just about any programming language I can think of except perhaps Cobalt we multiply with an asterisk So I'm creating the value of this fourth variable by multiplying those first three together. And then on line 22, we spit out the value. So this NSLog message, let's break this down.
That's obviously outputting some kind of string, some piece of text. In Objective-C, string literals, where you want to write the contents of a string of text in your code, are represented not just with surrounding double quotes. But with double quotes with an at sign at the front, and that's before the opening double quote. This at sign is yet another indicator to the compiler that, hey this is a bit of objective-C, not regular C. And hearkens back to the days when basic C didn't really have strings. It just had null terminated character arrays.
So this was something objective-C was bringing to the party. Now this is something that trips up all programmers coming to Objective-C. At some point, you're going to forget the at sign before the double quotes, and you'll get an error. That's it, it's not complicated. It's just something to remember. String literals like this need an at sign before the opening double quote. So what is this string? Well, I'm trying to construct a message that will spit out the value of a variable. In some programming languages, you might create something like this by combining or concatenating different pieces, like a little bit of text, there are, then a plus sign, then minutes, then a plus sign, then another little bit of text.
Whether you'd use plus signs or ampersands. But that is not what we do here. We use C style format strings. Instead of concatenating multiple pieces, we write our message with place holders where we want those variable values. Now place holders always begin with a percent sign. But we have different place holders for different types of data. So we need to tell what kind of data to go here. For us it is a simple integer value we follow the percent sign with an i. So now it knows that there is an integer value that suppose to go in the middle of this string but what's the value? Where does it come from? Well, we follow the string with a comma and then the name of the variable that will fill that place holder.
So, in this case whatever the contents of minutes in a year are will fill that string there are percent i in a year. If we had multiple placeholders, it expects multiple variables listed after there just separated by commas, and they just go in order. The first placeholder will be replaced by the first listed variable. The second placeholder by the second variable, and so on. Just as there are different kinds of variables there are different kinds of placeholder indicators. We've got percent I for integer.
There's percent F for floating point variable. Percent C for single characters. We will see these and more later. So for now if your new to these format strings just remember that all our placeholders begin with a percent sign. So why am I talking about these first? Simply because we're always interested in output. And being able to construct these will let us output more useful messages to the console. I can go ahead and run this, and let my code see that there are, apparently 525,600 minutes in a year.
Okay, this is simple but even when you move away from console apps to writing desktop and iPhone applications, you will still use NSLog time and time again to construct your own messages when developing and testing.
- Installing the tools
- Understanding the structure of an Objective-C program
- Logging messages and using format strings
- Writing conditional code
- Using code snippets
- Working with variables, classes, and functions
- Working with objects
- Using existing classes in the Foundation framework
- Managing memory usage
- Creating custom classes
- Working with Objective-C collections
- Reading and writing to files
- Understanding inheritance and NSObject
- Using Categories, Protocols, and Class Extensions
- Compiling and debugging code