[MUSIC] We just got done talking about structs. Now were gonna delve into our first discussion which is about code construct which is very uniquely, Objective-C. Everything that we've talked about up until now, if I'm not mistaken has been inherited from the C language. The way that we talk about objects is specific to Objective-C. That's not to say that other languages don't have objects of some kind, it's just that the syntax and the way of working with objects that we're gonna start talking about in this lecture Is unique to Objective-C. And in fact, the Apple development ecosystem. So we just talked about stocks and how they encapsulate data. But at a high level, what objects are, is they're variables that encapsulate both data and functions. To create a name, space and a scope in which data and functions can both be encapsulated into one unit. So there are some terms that we use, in order to talk about these things and the language becomes important for being able to communicate ideas and architectures among programmers. So we'll start with the idea, and some of these are general to any object-oriented language. In fact, all the orange ones in these slides are used in other object-oriented languages as well. Maybe with slightly different nuances. Generally, when we talk about a class, a class is the blueprint of an object meaning it's like the type definition of your object. An object itself is a class that’s been created. It’s used a little bit more generically to talk about, maybe not just the definition, but an actual variable of that class type that you’ve made. So an object is another way that we talk about it. An instance is a single object that's actually been created. So instead of saying, a variable, we say an instance of a class. This object is an instance of class fu, for example. Within that instance, within that object, within that class as the class defines it, we have variables, and those variables are called instance variables. The variables is called, or the object is called the instance and within it we have instance variables. That's the data. Also within the instance, within that object, we have functions and those encapsulated functions and object oriented languages have a special name and they're called methods. Data are instance variables. Functions are methods. And Objective-C, when we declare a class, we have to use a very special syntax. It's similar to what we've seen in C but it's a little bit different. In particular, one thing that changes is now we need two separate files in order to declare a class. We have a file whose extension is .h and a file whose extension is .m. You need both of these together in order to declare a class. The .h file is called a header file. And it's within this header that we see the definition of all of the instance variables that are present in a class. When the class is instantiated. When an instance is made of the class. It also contains a declaration of the methods. There's no code in the .h file. There's just a declaration of what the output type is and what the input parameters are to any method. Remember, we talked earlier about how sometimes there's a need for a declaration separate from a definition and this is one of those examples. In the .h file, we only describe what the method outputs, intakes, as inputs. We don't actually write the code yet. Now in the .m file, which is called the implementation file, that's where we put the code for the methods that are within our class. The header file starts with the keyword @interface and it ends with the keyword @end. It has the name of the class in it. And it has each of the instance variables, and those are preceded with the keyword @property. It also has each of the methods within it, and those have a slightly different syntax than we've seen with C. So let's look at that in a second, With this example of MyTime.h, so this is an example of defining an object that, Encapsulates the time that we've been working with. But it also encapsulates a function. Which is a method that's gonna calculate the amount of time since 1970. What we can see here is that there a lot of different syntax, words we haven't seen before for example the at interface which is the starting key word. That indicates the beginning of the header file, the beginning of the objects interface or its definitions without its It's declaration. At the bottom we have the keyword @end at the top we have the class name. Before each of our instance variables we have the keyword @property that's then followed by in parentheses nonatomic and nonatomic is something that's put there to handle multithreaded code it's kind of beyond the scope of what we're talking about now. It's basically necessary. It's a modifier for the property. That's then followed by a declaration of the variable that looks very similar to what we've seen previously when we were defining strux. The instance variable has a type. In this case, int. And it also had a variable name. In this case, month. The keyword dash, it's not really a word, it's a symbol. Dash indicates the declaration of a method within our class. And that declaration is gonna contain within parentheses the type that the method returns, the output type or the return type. It's gonna contain the name of the method, in this case approxSecondsSince1970. And if there are inputs to that method, those will also be declared after a colon. We'll start with, in parenthesis, the type of that input, the input type or the parameter type, followed by the input name that the method is going to use to refer to it. If there's more than one variable that's going to be passed as input, you continue to add them with a colon, type in parenthesis, and a name, finally ended by a single semicolon. In this case, approxSecondsSinceWhichYear takes one parameter, which is, whichYear. You can see that if you draw an analogy between our previous function definition here, float approxSecondsSinceWhichYear long which year, you can see that there's a mapping in which the parameter whichYear has the same name and it's info type is long. There is a disconnect there where the return type of approxSecondsSinceWhichYear float whereas in our method it's long. So the implementation file, the implementation file contains the code that implements the method. The definition for our declaration that was in the header file. [COUGH] Within the implementation file, we first see something called the compiler directive, so remember, this is a separate file. And because we have rules about scoping, we can't have access to any of the definitions that we've written in our .h file unless we tell the compiler to go look there and include that definition as well. So the compiler directive, #import MyTime.h, tells the compiler to go to this other MyTime.h and pretend like it was prepended to the beginning of that file. So that anything that was defined in MyTime.h is accessible within the MyTime.m file. So that's what that compiler directive does. Now, within the implementation itself. Where we do the definition of all the functions. We start with the keyword @implementation. And then we have a matching object name for the object that we're creating. We then see a repeat of the method signature that we had previously. So the -(long) approxSecondsSinceWhichYear:(long)whichY- ear. But instead of having just a semicolon at the end, we actually implement, we provide the definition of that method, otherwise known as the code. So here is an example of method which calculates approximately how many seconds since the year which is past. There are a few bugs in it, so don't go writing any production code based on this. For example, it doesn't account for leap years, it doesn't account for months that have don't have 30 days in it and also doesn't account for the fact that you may have a year which is in the future, for example. You may pass whichYear is in the future. The second method down there, within that method we have a local variable. It is a long, it's name is allSec and we initialize it to zero. If we want to refer to the variables that are part of the definition of our class that we saw in the MyTime.h file, in order to make sure that we're referencing the variables within the correct scope, we use the key word self to refer to that object in which the code is running and so self.minutes refers to the variable minutes declared in MyTime.h. We see that we're referencing self.minutes, self.hour, self.day, etc. And finally we have the ending keyword end. That second method there, approximate seconds since 1970, calls the previous method in a special syntax which we'll talk about in a future a future lecture. With the parameter 1970 mapping to which year? And so that second method hard codes the year from which the difference is taken to 1970. Finally, when we wanna use this object, we use a syntax like this. We say, MyTime *.a. We have yet to explain what that star is about, that's coming shortly. A syntax that creates a new object called MyTime. And then we can access each of the elements within the object the same way we did within a struct, a.month, a.day, a.year, a.hour, a.minutes, a.seconds and then we can print the answer to how many seconds there were since 1970. You can see the output down there at the bottom. So in summary, objects are a programmer defined type, also, like structs. But they encapsulate not just data, but functions. Instance variables and methods, for when you just don't want to move data around, but you wanna move algorithms around that operate on that data. They enable programmers to treat data and functions as a unit or methods as a unit. And the scope of those instance variables, can be accessed if you used the self keyword within the definition of your methods for that object. Great, that's an introduction to objects in Objective-C, our first foray into pure Objective-C syntax. [MUSIC]