From the course: iOS Development Tips

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

The ? and ?? operators in Swift

The ? and ?? operators in Swift

From the course: iOS Development Tips

The ? and ?? operators in Swift

- [Instructor] You might have seen some Swift code that has question marks in it, and have no idea why that was. Let's deal with two really useful operators, the ternary conditional operator, and the nil coalescent operator. If you download the exercise file, you'll find a playground that you've probably seen before, it's a simple if then else, now for multiple lines of code, this what you'd want to be using but there are times when you're just changing a value like the one I've got here where I'm only changing between a good and a bad pizza. For these cases, you can use a ternary conditional operator instead, with a single line of code and only one assignment. So I'm going to change the rating here underneath this to seven, just so we can see something, and I'm going to assign a new pizza rating, so I'm going to do pizza rating, okay, and then I'll put an equals, and then I can put a bool expression and a question mark, so I'm going to do the same one I did here, if rating is greater…

Contents