Optional Chainings in Swift

Bob Lee
A free video tutorial from Bob Lee
iOS Instructor | Bob the Developer
4.7 instructor rating • 1 course • 1,579 students

Learn more from the full course

Learn Swift 4 with Bob: The Intermediate to Advanced Swift

Functional Programming, Protocol Oriented, Value Oriented, Generic Protocols, Object Oriented, Swift 4

17:38:08 of on-demand video • Updated September 2017

  • Learn the difference between classes and structs and which one to use
  • Learn how to create generic enums, structs, classes, protocols
  • Get a taste of functional programming with trailing, auto, and escaping closures
  • Understand how objects are created and removed through Automatic Reference Counting
  • Understand potential retain cycle in closures, delegate, and class variables
  • Understand Swift is a protocol oriented programming language
  • Apply protocols such as Equatable, Comparable, Sequence, and IteratorProtocol
  • Execute Swift files using Terminal/Command Line
  • Functional Programming
English [Auto] Welcome to listen to the fundamentals in the last lesson you've lore Why is optional. And then we learn how to convert to normal types to work with each other. However you may have wonder why those question mark and exclamation mark those Bankside automatically appear when you try to access a property or method of an object. If you've never done it before that's OK. Goal of this lesson is prevent you from guessing around but just find out what goes under the welcome to of all Channing's. Here's a proud statement. Why do I see a question mark and exclamation mark when accessing methods and properties. By the way if you don't know what methods and properties are this this is not the right course for you. Again in the part of the prerequisite you need to know what the basics of object or programming what it means to initialize initialize an object. OK so here's an example from the kit framework. If the devil needs we have a little bit of experience with. I was Lattman you don't. Def You don't half know but it's a good idea to know all the Aiwass ecosystems such as View controllers anyway. So we got to make Elm initialises I label. And then let's get the property highlighted text color. All of a sudden you see this weird question that appears out of nowhere and you get C.G. color propert property again. But why does it. Why is it happening in the first place. And if you have ever wondered in the past now you don't need to guess. Are you going to find out why this is occurring. And let's try to replicate the feature. Let's try to make this happen in the first place so what is definitely what what is going on in the first place. So I'm going to create a design a class called human. It contains one property code name and also one method to call say hello. OK so it's nice and simple just like that. We have a human class and again you need to know what it means to initialize a property. OK OK. If not this is not the right course. Please go to the YouTube channel my YouTube channel you have free access to. Introduction to object programming. You know what it means to use our properties and methods and I'm sure most of you guys have gone through that prerequisite and I'm not going to mention that this is an intermediate divel of a swift course. OK I'm not going to mention again. I assume you guys already know this things. OK so we have this human class and that we are going to design another class call apartment. And if you know apartment apartment contains a human property right. Every apartment there should be a dog or there should be a person living there. OK. So hopefully that makes sense to you. And in this apartment Clauss there is a property called human and the type is humans that means you have to initialize with them with a human object. Right a type. And this is Neal you can put it as Neal you don't have to initialize it because this is a natural type as you know optional type you can simply call it Neal. You don't have to do with the sounds all get Ogger. So now here's this trick what I'm going to do is I'm going to try to initialize the proper initialized the apartment still initialize it initialize it and let's call it. Since I live in Seoul right now on the coldest soul apart men call it Bart. And then let's call the apartment like the case you've created this apartment object. Then of course there's no property here right. I mean you have not initialises human property. So even though there's apartment the human property or there's nothing. There's no value to it just natural value. But then you're going to know. But this time you're going to call it human. And here we go. Then enter something and the value going to enter is human. You to create this object called Barbie or Barbie whatever you want. Put your name on it. OK. Now the Seoul apartment human has been human property has been initialized by with this object. Human. OK. So now here comes a magic happens. All right. Drum roll please. Anyway let's go solar Parman da human the human. Here we go. We have this call say hello method right. Right. One two three. Boom. You see that right there again machine one more time. The human dart. Let's get the name here right. Take a look at this focus. One two three. Boom. Oh what happens what's happening. This thing is being added automatically. And let's take a look at why this is why this behavior occurs in the first place. Well if you take a look at there's a human actually is a property of apartment. And this is an optional type. So in swift What happens is if anything is offshore type the question mark is automatically added after an indicating that what comes after may not exist. And also the fact that human property is also optional. It tells two things a human object is a human property is an optional type. And also what comes after can be an art. Neal here's why. So right now human name is definitely a problem Bobbie. Let's take a look at this. Let name equals human name sounds so good. Now we have a name Olguin is going to Bobby Lee on the right side. But the problem is this is an optional type. So anything comes after optional would be optional. Why so. Well here is exempt here. Here's another example. Lasy. There's an apartment called New York City apartment. I lived there for all my for my college. It's pretty expensive right. So Naoki apartment dot. If you try to get human name. We're going to have is nothing you're not going to have anything right because there is no there's no value there's no object associated with human property. So it's good to have Neal. So if to to anything it comes after optional property will be we will possibly arbitral value Civic if human contains Neal. They'll definitely there have been there. The name can be awesome Neal as well. So anything that comes after optional type the question mark going to be Neal. OK so here's the thing. Again there are two ways to go about this. There are two ways to unwrap this. The first way we talked about it if you talk we talked about the free first lesson the first one is forced unwrapping while we can do something like this right there instead of putting a question mark you put an exclamation mark right there. So now the property now the type of name becomes string automatically. OK Sue good. So you're able to work with this name. All good. Everything's fine. So if you put an exclamation mark anything comes after will be optional type because you know the human will exists. But here's a problem. Here's a problem. What if you try to do something like this human exclamation mark that name again you can see this problem found Neal while on mapping an optional value. Well if you know human is optional type right but you're not allowed to convert Neal to normal type using for Neal we talked about it in the previous lesson you're not allowed to do that for Neal value normal type. Definitely need to have value associated soon and not to do that. So this is force or mapping force on mapping and how you can simply use. Now implicit in mapping you can use implicit mapping on wrapping and wrapping means converting converting optional type to normal type. You can simply use and the conditional statement you can see if flat let's call it reds the name equals Nusi apartment god human dog. Name print resonate. And if there is Neal if if name is Neal to say no name available available right there. So right now if you know apartment the human the name will be nil. Right. Because you have not initialize you there is no value for human there's no value there's no object associated. But if you decided to New York City apartment a human Let's create and assign an object like this. Let's call it bubbly. And then now here comes bubblin right there. Reza name print bobbly on the bottom. And if you don't do like if you don't do if you do not initialize it no name available. But the good thing is is not going to crash right. It's better and better than using this way of unwrapping it. No that's a no no. We don't want to use this. Not the ideal case. We don't want to use as we always use implicit save unwrapping. This is a better way to go Bill. So don't use it. OK. I mean there are times you want to use it if you want to make something real quick or this is pretty long and this is proof Sure depending on the context of how you're programming. But most of the time this is a safe way to go about and this is this is the right approach and this but sometimes I'd certainly use force wrapping. But when you are 100 percent sure that the value exists. OK. Ngwe. Hopefully you understand by now why these things are automatically added. So this is literally just still optional property. And if you click on it you like color is a type just a normal a just a type just optional property and anything comes after will be Opsahl optional right. So if you want to you can also do this. But it's going to crash I believe. Yeah because there is no value there's no value of any way. So Hovey has learned something new less in the next lesson. Both sea floor all all changing provide like a shortcut to access nasty properties and methods of an object by the way if you hear some sound of cars coming on the left side right. I'm sorry about that. Now you've got a new studio here right in the middle of the road so hopefully I can move out soon in the future. Let's go back to Officer Cheney. However those question marks are added to a property has type is optional to indicate that anything comes after that potentially nail those optional value in the next lesson you will learn how to use a car statement to safely imagine multiple options without using classic if lead and you'll discover why you used car statement instead. Again if you want to learn how to increase your productivity when it comes to coding make sure check out those articles I posted in the lecture notes. See you in the next lesson. Bye.