[MUSIC] Okay now that we have these very nice classes that have been declared, the question is how do we use them? So we have to add an objective-c, so perhaps we can see the use cases for these. >> Yes, so let's instantiate these classes, create some >> Define some cities, and create some countries. And we'll do this in this random class file called app delegate. And because it is a good place. >> It's a default file that's included with every IOS project. It should be in every app you create. >> Yes. >> What it really does is not important, at this point. But it's just a convenient place to load our data. We'll create just a temporary function there to do that just so you can see how it looks. To create, do some literal values. >> Yeah. >> Instantiate objects. >> So, yes, don't worry about that delegate right now and we'll cover, we'll definitely cover this in the future and I'm gonna pull up this. Objective C, blank app I created, and just this deal that the app will get from there. So, I'm just taking objective C rating so we can have the comparison. And again, as you can see the interface, I mean, maybe we can talk about their protocols here. [CROSSTALK] So, in Swift you've seen that this is like sub-classes and protocols. There is no difference in a similar syntax. Or in objective C this is the subset I have delegated a subclass of UIresponder in both of these cases. But it also conforms to the UI application delegate protocol. And in Swift as we have seen before you simply put that in a >> This after the subclass. Or in Objective C you'd give it this nice angular bracket thing. Which is pretty nice too, I like that as well cuz it's more clear that this is a protocol, or this is also a protocol. >> But it's a similar idea, protocols in Objective C and Swift work quite similar. The kind of general idea is the same. All right, I'm going to do this function, too. All right, so let's create some cities. We're going to create a function called load. >> Load cities. >> Load city. >> Or load country. It doesn't matter, load stuff, [LAUGH]. >> Load stuff. So far. And so we're gonna create a >> So, in Swift we've done this before so let's just create a bunch of cities. And we're going to create an array with a few things in it. >> That's cuz our initializer for the country wants an array of cities. So, we're working backwards here, we're gonna make sure we have an array of cities first and then give that to the country. Cuz it wants a bunch of cities. >> Yep. So Toronto is a great city. It has a population of three million. Yep. >> Give or take. >> Give or take. >> Give or take like ten million. >> Yep. And [LAUGH]. And then Vancouver's also a cool city. Vancouver. [INAUDIBLE] population of, like five, give or take 10 million. So here's a cities array, and we've seen this before. We're instantiating two cities, Toronto and Vancouver. And so let's Before we get any further. Let's do this in [INAUDIBLE]. So, we're going to have a void function, which means it doesn't return anything. LoadStuff. And we're also going to create an array of cities, which is gonna be NSArray. Similar style. Ooh, why can't I create a UTCity? >> I think you forgot to import something there maybe. [LAUGH] >> Oh, geez. [LAUGH] Because in ObjectiveC not everything is available across the whole module. So, if I want to use Like in C and C++ with imports and includes, you have to tell the compiler that, oh, I'm gonna use something from this file over here. >> And that's what that h file is important, because that's all we know about our UTCity class. Is that .h file. >> Yes. >> So, we can UTCountry, which we shimmied in there as well. >> [LAUGH] Yes. >> Normally you'd have separate files for that. >> Separate. So, now I can see the UTCity class, and I have to call alloc on it, interesting. And I can call on it with name, Toronto. >> You have an extra square bracket, there, at the beginning. >> Hopefully it can add it for me. [LAUGH] Why did I do this? >> Nested method calls basically happening here. >> Yes, so as you can see this is already wrapping, it's to long. Let's give our city more space since it seems like it needs it. There you go and another city Vancouver, that's a population of five. All right. Take a look at this. So, in swift, very clear again and we've seen so far. You have a city with the name of Toronto and a population and this returns. Sort of the city object that you want pointing to this array. Whereas, in objective C we are doing a bit more stuff. First, we are calling alloc on the class which, I mean it sounds like malloc. It's allocating memory which is what it is doing. So, it is just giving you a block of memory to hold city and at this point you don't know what the memory is so you have to initialize it. And this is just very Sort of supporting c. Again, Objective c is a super set of c. You have to do things in the sort of c mindset often. Yeah, you remember you're allocating dynamic memory on the heap, and [LAUGH] it's a little bit more explicit about these behind-the-scenes things, and a bit more. You know, Objective-C's quite an old language now too. This has probably just been around for a long time. It's quite a legacy type thing, so yeah. >> And also, what's up with these, @? >> Yeah, so because C and objective C are compatible, objective C code has to work understand what C code is. And C if you have just two quote a quote that's a string a C string a null terminated C string of characters. It's non objective C string. So, Objective-C has this symbol that it understands, the @ symbol, which generally means you're indicating an Objective-C object. So, when you put @", and then a string, you're actually creating an NSString, which is what we want here. >> Mm-hm, and similarly, for the @ array, it's just >> Convenience to know that we are creating NSArray here. And everything else is more or less similar. All right so let's create our country. So, can you guys guess what country this is that has Vancouver? It is Canada. If you say so. >> [LAUGH] >> We should still have done Antarctica but definitely next time. >> [LAUGH] Canada [INAUDIBLE]. >> In our unit testing code, maybe, with an edge case. >> [LAUGH] I think it will be the bane of this program. [LAUGH] So, Canada, There it is. And we're using UTCountry. And again we have to call alloc So, it's not too much different here between this example and the previous example. So, let's move right on to >> I mean, ultimately what's happening is the same. We're creating an instance of an object. It's just, there's a little bit more boiler plate, like, it goes around the Objective c version. But, ultimately it's the same kind of idea. It's allocating memory, setting up initializing the variables and storing it in a local variable. >> Mm-hm. And so finally I guess we want to find the city with the largest population? >> Sure. [CROSSTALK] Or one by a name maybe? >> By the name, sure. Let's do it by the name to give Vancouver a chance here. >> Okay. >> [LAUGH] So, if you wanna find Vancouver >> Which would be Canada. Anyone can just call findCityWithName. Vancouver. All right, but then, this will be an optional city. >> It's maybe Vancouver. >> Yeah, exactly. >> [LAUGH] [CROSSTALK] >> It figures it out first, all right. >> Depending on if we spelled it right, cuz when you go to you might miss out with the u, and then it wouldn't be Vancouver. It'd be American Vancover. The inobjective c, we're also gonna Create a country. It needs a city. And again, no type reference here. >> Yeah, we have to explicitly name the types of all these variables too, because it doesn't interpret. Like, it can't figure out that Canada is a UT country for example, or Vancouver is a UT city by what you're assigning to it. Yeah so. >> [CROSSTALK] that type inference right? >> And honestly it doesn't seem like cares. So, here we are telling the system that Canada country. And that allows us to the class here. But when you are actually using it, it is really weak in terms of If it cares or not whether it's actually a country, or if it's just a generic pointer to anything, and that's why we always have to put in type name as well. All right, so that's how we call this function. So, if we want to get the largest city, in Swift it's very similar. We just It's always using a dot. With population when objective c. There's really two ways we can do this. Right, we can go largestCity = canada.cityWithLargestPopulation, which is surprisingly similar to swift. >> Cuz it was a property that we declared, right? >> Yes. But there's also another way we can get, LargestCity2 which you can also call that scatter as a function. >> Right, cause that's what it is. >> Cuz even when you're doing the sort of dot syntax, and calling our property, what it's really doing is calling the scatter >> Behind the scenes. So, if you're just calling together here these do exactly the same thing. >> Yeah, because before you had to even, you didn't always have those dot syntax in Objective C. That's a new thing on the pass book, you know, five, Objective C 2.0, whenever that came out >> More than five years. >> More than five, oh my God. [LAUGH] But, Yeah, so the dot syntax is really just sugar. It's just a nice way to access that method, because some people like that dot syntax. It makes them feel comfortable and happy. >> Yeah, because imagine if this did not exist. Look at all of the square brackets we have. Some people are okay with it. Some people are maddened by it. But, really, the dots and tags, it's just hides that call to that method. That's the difference with Objective-C. Where as Swift is, that dots and tags is more fundamental to the language. >> Mm-hm. It works more like, well I wouldn't say more like, but in C++ when you do it, Dot, again, in C when you do a dot, you're accessing something directly, so, but in Objective C, the dot is sort of the arrow operator in C++, where you're dereferencing the memory and then getting a property. But then, Objective C, the dot can be either the arrow or the dot, whereas Swift, you just have the dot, which is very good. >> Now I see some warnings. What are these warnings trying to tell us? >> These warnings are Xcode trying to be nice and telling us stuff, that our variables are unused, which is true. We created them and we haven't used them, because it's more of a demonstration. So, it also gives us different, so Swift has a new feature which, if you don't care about a variable, You can not give it a name, which is very interesting. So, you can call this method and get the return value, but you just don't care about it. So, you just want to put into a underscore, which we talked about. And you can do this in function cranrate and such. But in Objective C you can't do that, so it's just gonna tell you you haven't used this yet, which probably means you shouldn't be printing. I'm sure some people, including myself, have a question of how is this useful if you are just calling. Is it that this essentially has an effect and that's all you care about? >> Yeah. So, say this had a side effect. Which is doesn't. Here, you can always just call it without an assigned return value. But this underscore is really more useful as we coded be for when you're say getting a block which gives you three parameters. But you only really need one of them. So, the other two can be underscores. So, you're expressing clear intent that I'm going to use this and I'm not gonna use these. Even though you're gonna give it to me, I'm gonna sign it to the underscore, which means I'm just not gonna use it. But you're still gonna give it to me anyways, right? When Objective C, you can't express that meaning in Objective C. So if you have a function that gives you five parameters, five variables, in Objective C all five would be ran out and you would have to look in the code to see which ones you're actually using. In Swift you can express that I'm going to just use this one. Or none of them, and give them like five underscores, cool. Okay so what we've seen is a basic of Objective C is SWIFT, there's a lot more to Objective C obviously and so we'll have links and for anyone who wants to learn more about Objective C. It's good to have a look, because there's a lot of code out there, and there's quite a bit in objective C right now, so it might be useful. But for the remainder of this specialization, we'll just focus on [INAUDIBLE]. >> And I hope, after seeing this comparison, you're much more happy that we are indeed focusing on >> I don't know. I still like Objective-C, but maybe that's just cuz I've been programming in it for a while. [LAUGH] So it's still cool. >> Yeah, no, I definitely urge everyone that's doing LS, eventually you're gonna have to give yourself an Objective-C, it's unavoidable, really, at this point. And maybe if, five years down the road, you can avoid looking at Objective-C but since >> But sense iOS and Cocoa Touch, the framework we're using, is random in Objective C, like, lots of the restrictions do surface in Swift and we see them all over the place. >> Well, I guess if someone wanted to build a simple app you could actually build that app with Swift. And we're going to see that throughout this course. But if you want very advanced functionality, then in those cases, some of the codes you might actually find online, or you may want to use, are in Objective C. So that's something that, again, goes to the fact that this specialization is geared towards, maybe, the more beginner, moderate type programmers and app developers, but Very advanced, perhaps should be much more Objective C savvy. [LAUGH] >> Yeah, and I guess we can just briefly mention how Swift and Objective C works together in iOS, because as you can see there are two languages, and can do the same thing in both. And like Prior mentioned, there's a lot of existing Objective C code out there. Well, in Outpoint you do Swift they're not gonna Just discard all that code. So they created many ways, which is out of the scope of our course, for ObjC and Swift to work together. As you see, just like ObjC Bridging-Header here which allows ObjC to be used in Swift and that's And you may run across that in the future in your codes, and there's lots of resources that will provide the course materials that goes over that topic. >> Great. So don't just ignore anything you see that has an objective C. You can still use it. [LAUGH] It's still valuable, right? >> Yes. They're very valuable. >