Swift - Day 6 : Basic operators
Swift’s operators are mostly similar to that of common programming languages, but there were a few that stood out for me which I thought I should write about today.
The simplicity of this operator really stood to me and it is one that I suspect I will be using a lot considering that Swift uses optionals which often require checking whether a value is a nil or not. The Nil-coalescing operator does this in a much cleaner way.
Instead of checking and assigning a default value like below
Nil coalescing has a cleaner way which is
Range operators are common in other languages, I’m writing about them because I tend to mix them up, but I’m certain this will improve the more I use them.
Closed Range operator
(a...b) This defines a range that runs from a to b, and includes both a and b.
Half-open range operator
(a..<b) This defines a range that starts and includes a but doesn’t include b that’s why it’s said to be half open.
(a...) defines a range that starts from a and continues as far as possible and
(...b) defines a range that starts as far as possible until b. This is called a one-sided range because the range has a value on one side.
(..<b) defines a range that starts from as far as possible and ends at b but without including it. This is a half-open one-side operator.
Most of the operators in Swift work similar to how operators work in languages like Java and C#, this are a few that stood out for me when learning Basic operators in swift.