Join Simon Allardice for an in-depth discussion in this video Data types in Objective-C, part of Objective-C Essential Training.
Well, not in Objective-C. There are ways of having flexible variables in Objective-C, but to recap, for the most part we define variables something like this. Every variable having a type and a name, and we provide it that type when we create the variable. We can also provide it a value at the same time that we define it. So, to create a variable that hold the integer, we use the keyword int. Not integer, but int. So that's the type, and it's lower case. Int is part of the language. Well actually, int is a keyword from C, so it's automatically part of Objective-C.
And we'll talk about the other types over the next few minutes. Now we then name the variable. The name, of course, is up to you. You give your variable a meaningful name. Sure, when we're playing around with code, we create variables called a, b, and c and x or foo and bar. And this is because right now the names don't really mean anything. But in your programs, they're going to represent high scores and account balances, names and dates, latitude and longitude. So, we name them so they make sense later when we or somebody else reads this code in a year.
Every programming language tends to develop its own style guide lines for naming variables. And we will talk more about style guide lines later in the course, but for now, in Objective-C this is what we do. We name variables with camel case, we begin lower case, if it is just one word it is all lower case. If there are any subsequent words, we start each one with an upper case letter like the hump of a camel. Variable names are, of course, case sensitive. All of Objective-C is case sensitive. So these versions of this score variable would be considered different variables, but it would be a terrible idea to do this.
Now, don't be afraid of having long variable names. We have code sense in Xcode that will help us automatically complete long variable names later on. Now, in naming variables you can use letters, you can use digits. You can also use underscore and the dollar sign though you can't start with a digit, something like this. First name with a number, one right at the first letter would not compile. Now, if you aren't coming from a language that has some other naming standard like prefixing variables with their type or using underscores between different words.
And now know that there is nothing that would stop you from doing this in Objective-C, these would certainly work. But it's not what Apple would do or recommend. And that's important because you're going to need to read Apple documentation, and view sample code and read online discussions, and everyone will pretty much name them, camel case. So jump in, try and name your simple variables like this, even if you're not used to this format, and it will make it easier when we come to naming other parts of our program, like methods and classes.
So, what other types of data can we have in these variables? Because Objective-C is built on top of C, we have the same data types as the C language, things like int, float, double, char. Now int we've seen and there is a little more to integers that we'll cover in a moment. Along with float, and double these are floating point number. Char has single character. It's just one letter at a time, one character. In Objective-CV also have the boolean data type written a little unusually as BOOL all in upper case.
And we'll cover all of these in the next few minutes. Well, that's pretty much it for basic data types. Well, hang on you might be thinking. What about dates and strings of texts longer than one character? What about images or buttons or menu items? Well, these are built into the C and Objective-C language. These are those primitive types. And many other data types are really combinations of these primitive types. A date after all could be represented by several integers one for the year, one for the month, one for the day.
Or you could even just represent something by the number of seconds since the 1st of January 1970. And you might think I'm kidding, but this is UNIX or POSIX time. This is a very common way to represent a date and time on a UNIX system. That means we can hold date and time internally in a single integer. And when we work with text, or strings, or any piece of text is essentially a bunch of chars. A bunch of individual characters taken together. So, a date is not a built-in primitive data type in the Objective-C language.
But, as I've already mentioned, we have those data types available and many more. They're part of the frameworks we use, they're not part of the language itself. So, there are string and date types. Well, actually called NSString and NSDate types, that are classes that have been written using Objective-C and made available to us, but they're not like primitive types like ints and floats. And the process of creating and using variables that are based on these types of NSStrings and NSDates and many others, are very different from creating ints, floats, chars, BOOLs.
So we're going to get to these. But we're going to get to them later. Let us begin with the primitive types and build on those.
- 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