[Swift] Day01: Basic operators in Swift
Today, we mainly look at the basic operators in Swift. Record it.
Null Value merge Operator (Nil Coalescing Operator)
a ?? bIn??Is the null value merge operatoraIf notnilOtherwise, returnb.
There are two requirements for use:
-A must be optional
-B must be of the same type as.
That is to say, a must have the possibility of being prepared, and B must be qualified to be prepared.
In fact, it is short for the three-object OPERATOR:
a != nil ? a! : bOra == nil ? b : a!
Interval Operators
Interval operators are divided into closed intervals (...) And left-closed and right-open intervals (..<). The former is the head-end calculation, and the latter is the head-end calculation.
The interval operator returnsRange An object is a set of continuous non-correlated sequence indexes.
Which of the following statements was previously left closed and right open?..This is exactly the opposite of Ruby...
But some people just want to use it...In this way, you can write one yourself:
infix operator .. { associativity none precedence 135}func .. (lhs: Int, rhs: Int) -> Range
{ return lhs..
You can also use generate () to traverse:
var range = 1…4var generator = range.generate() // {startIndex 1, endIndex 5}generator.next() // 1generator.next() // 2generator.next() // 3generator.next() // 4generator.next() // nil
.generate()ReturnsRangeGenerator Struct, which can be used to traverseRange .
There was(5...1).by(-1)But it seems useless now.
Iii. Others
Well, if you have a limited time today, write this first.
In general, there are almost no differences in operators, but the Range part is a bit like Ruby or Haskell syntax, but Range can be used in Haskell as follows:
ghci> [‘a’..’z’] “abcdefghijklmnopqrstuvwxyz”
In Swift,...These three points seem to have concatenated strings:
Let bb = [""... "Z"] println (bb [0]) // print ["... Z "]
This is the case,...You can splice two strings:
Let bb = ""... "Z" println (bb) // print: "... Z"
Well, today we will go to bed first.