Muhammad Harits Syaifulloh

Wednesday, October 26, 2016

Swift 3 - Optionals In Swift 3

Swift 3 - Optionals In Swift 3

Occasionally we need to have a variable that we don't want to write too immediately, we might write to it later on through. In most programming languages this causes two problems. The first of which is that nothing is stored then we get what's called a "null references", and this simply means that nothing is contained in the variable. This is normally when a crash occurs and this crash happens when your user is running the app, so it's not good. Secondly we need to add a check for now which means we need to write more code and write more carefully.

Swift does away with this quite elegantly, but it is a little confusing at first. So let's get rid of that confusion by following along and looking at optionals inside of our playground.

Now let's have a variable called myStr which is type of String but we're not going to give this variable a value right now. Instead we gonna put a question mark after it and we are telling that this variable is optional, it might exist and it also might not exist. And in this case it has two sets or values that it can have one of which is nail which means it has nothing, the other is a value and that value contains the actual variable itself.

So think of optional as a special data type or it's not the special to honest but I like thin of it as special that contains either nothing or something.  And if it does contain something we can get the value back.

Let's try to print the variable and see what our compiler says, so have the following code :

Alright, it tells us that we have "nil" inside of our string. Now let's try another one, so have the following code :

And now we have an error and that is because value of optional type string is not unwrapped, sis you mean to use "!" or ??. So now we just got a crash or error, some fatal execution there. And that because we don't actually have a value inside of our myStr. And luckily for us, Swift or Xcode telling us that they have an error now and if you have this error it won't let you compile the app and release it it's impossible.

So now let's do the correct way to use optionals in Swift 3. So what we have to do is have a statement like the following code:

So the above statement will run if we a value in myStr and you can see how are avoiding those nasty "no references" or "nil references" error now. So I can now assign a value to myStr :

And now let see at the right-hand side of our playground, it's now print out "Learn Swift 3 with Ask MHS" because we actually have a value. You also of course now know what "?" or question mark means and again when we come across it, I'm going to explain what it means in terms of the item we will be working on at that point in time.

So that's all about Optionals in Swift 3. Thank you vary much for visit my blog and if you have any question just leave some comments below and I'll replay it soon. See you next time in the next tutorial.

No comments:

Post a Comment