Swift question mark | Sololearn: Learn to code for FREE!


Swift question mark

I'm still confused about the ? in swift . I even googled what it means but I still don't understand it. Can someone please help me?

4/11/2020 8:54:34 PM


5 Answers

New Answer


The question mark(?) can be used as a ternary operator. The kind of expression it does works much like if-else statement. In other circumstances, the question mark can be used to declare the type of a variable as optional type. This variable may or may not have a value. Perhaps if you provide a piece of short code you struggle to understand, I could help you better. Here is more about optional type and how to unwrap it with exclamation mark(!). https://medium.com/@agoiabeladeyemi/optionals-in-swift-2b141f12f870


Thanks ChillPill


Thank you bendjebbar


//You can also use it (?) like this exemple : let x = 10 // while x < 20 , i take value of i else i take value of 2*x var i = x<20 ? x : 2*x


This operator is available in many languages, not just Swift.