Swift: Why We Use Optionals

preview_player
Показать описание
Today let's go through a relatively simple exercise of finding the greatest value inside a list of Integers. I'd like to use this example to explain why we use Optionals when programming in Swift for our iOS applications. Basically the intent is to remove ambiguity from our programs as much as possible. Enjoy.

Instagram Firebase Course

Facebook Group

iOS Basic Training Course

Completed Source Code

Рекомендации по теме
Комментарии
Автор

Nice video:) One small improvement I would make is after checking that array has more than 0 elements, you could just assign first item from the array to greatestValue (instead of -1).

fengson
Автор

my guy, u are literally my teacher right now that im learning how to code in swift every time I'm stuck or i don't understand something i can count on you to explain it with clarity

jonathanhernandez
Автор

I'd happily be corrected below:
An optional is used when a value CAN be nil. A Facebook post for example CAN have comments but it CAN ALSO have none what so ever (nil). When calling a value that CAN be nil in your code, it is VERY wise to safely unwrap it because your code needs to know what to do when there is data, as well as when there isn't any at all (nil). If you force unwrap it, it will only work when it isn't nil, otherwise you will get a crash because you've forced it to execute something when there simply isn't anything there (it's nil). Your code just doesn't know what to do when nothing is there.

Daniel-szlq
Автор

Cmd+Click to go to the documentation was something I didn't realize you could do. I always relied on the help sidebar. Thanks!

stevenr
Автор

The documentation saying smallest is a good indication that someone copy and paste from the min method lmao. Awesome video!

nobytes
Автор

Optionals are something powerful to use in Swift for iOS programming, to me.  I actually find other programming languages lacking in this area.  Thanks again Brian!  great tutorial as always :)

JustVictor
Автор

Thank you Brian for this great video on explaining optionals!

I think your algorithm will not work for a list with negative numbers smaller than -1. For instance, if you pass the list [-5, -4, -3], your function will still return -1 as the greatest number, which is not true. I understand that your whole point in this video is explaining optionals and not the best findGreatestValue algorithm. But I just wanted to improve it.

Once again, thank you for the video.

muhsinzahidugur
Автор

Very good exercise on explaining optionals! Thanks!!

theodorefields
Автор

Great video. I understand why optionals are important, but I find that I avoid using them if possible because I don't really understand how to use them. A video on how to implement optionals in a project and all the different symbols involved (like ?, ??, ! etc) would be really helpful. Thanks!

dmmm
Автор

Nice topic, very useful, I would like to recommend another one to talk about. "Groups" vs "real folder on disk". I thinks that is a very confusing content and I would like to know which's the most appropriated way to work, best practices and if is there any way to enhance the .ipa file size using one of them.

dairdr
Автор

Watching this at x1.5 is saving me so much time.

faisalsal
Автор

Thank you so much! Could you make tutorial for TableView or collectionView to get last 10 new data from firebase for example and when you scroll up or down get more data? Like Tableview optimization for memory etc, please?

salahqx
Автор

Hi Brian, I enjoyed this video and have subscribed! Just to clarify, the optional allows the variable to return a nil value, instead of a hard-coded value (in your example negative 1). Thank you for your time responding to this comment :)

sanjitraman
Автор

Optionionals! Making everything needlessly more complex than they ever needed to be.

zavatone
Автор

Helpful. I'd love a short vid on the use of ! in this same vein.


BTW I think you were reading from the documentation for min, not max, so they actually had it right.

markw
Автор

It seemed at the the comment about smallest was valid because the code was for the implementation of min() and not max()

JohnDavis_NetSkink_Computing
Автор

Just wanted to comment first - Love your videos!

sephmoroth
Автор

I hope you will create awesome course for swift 4 one day waiting for that day

arjunneupaane
Автор

You are awesome.
Your videos is so helpful

alshezawi
Автор

I was watching your TV in the background, the whole time. Nice explanation though.

rajatsaxena