Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

"!on" is a negation, what about "on!" is that the same as negation or different? !

perhaps this is unrelated a != nill ? a! : b

this is a coalescing operator ? that is kinda like var usercolor = "red" var colortouse = usercolor ?? defaultvalue so if user color is set to nill then default value is chosen

so "a" value of a is not "!=" "nill" null value "?" choice based off left side and right side "a!" nill value a or is negation? could it be !a too? ":" then "b" choose b

so "?" says if has a value choose a, if a is nill choose b

1 Answer

Hello !

!on is not the same as on!. One is a negation like you mentioned, and the other one on! is to force unwrap an optional.

Here is an explanation of the Ternary Conditional Operator.

Ternary Conditional Operator

The ternary conditional operator is a special operator with three parts, which takes the form

question ? answer1 : answer2

It is a shortcut for evaluating one of two expressions based on whether question is true or false. If question is true, it evaluates answer1 and returns its value; otherwise, it evaluates answer2 and returns its value.

And

Nil Coalescing Operator

The nil coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil. The expression a is always of an optional type. The expression b must match the type that is stored inside a.

The nil coalescing operator is shorthand for the code below:

a != nil ? a! : b

The code above uses the ternary conditional operator and forced unwrapping (a!) to access the value wrapped inside a when a is not nil, and to return b otherwise. The nil coalescing operator provides a more elegant way to encapsulate this conditional checking and unwrapping in a concise and readable form.

Hope this helps.