The main things to see today are the basic operators in Swift. Record it.
Null-value merge operator (Nil coalescing Operator)
a ?? b
In the??
is a null-value merge operator thata
Be judged, if not fornil
The package is unpacked, otherwise it returnsb
。
The following two points are required when using:
-A must be optional.
-B must be consistent with type a
In other words, a must have the possibility of being prepared tires, B must have to do the preparation of the tire qualification.
In fact, the shorthand for the three-mesh operator:
a != nil ? a! : b
Ora == nil ? b : a!
Interval operator
...) And left closed right opening interval (.. <
) Two, the former is the end of the calculation head, the latter is the head is not counted tail.
range<t> Object, is a collection of contiguous, non-associative sequence indexes.
.. &NBSP, this is exactly the opposite of Ruby's ...
But some people just want to use ..
&NBSP, then you can write one yourself:
infix operator. {associativity None precedence 135}func. (Lhs:int, Rhs:int), range<int> {return LHS. <rhs}for I in 0..10 {println ("index \ (i)")}
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()
Returns aRangeGenerator<T>
structure that can be used to traverse theRange<T>
The value in the.
There used to be a (5...1).by(-1)
usage, but now it seems useless.
Third, other
Well, today's time is limited, so write this first.
Generally there is not much difference in the operator aspect, but the interval part is somewhat like the syntax of Ruby or Haskell, but in Haskell the range can be used as follows:
ghci> [' a '.. ' Z '] "abcdefghijklmnopqrstuvwxyz"
and in Swift, ...
These three dots seem to be stitching up the string:
let BB = ["A" ... "Z"]println (bb[0])//Print ["A...z"]
Test it, indeed, ...
can stitch two strings together:
let BB = "a" ... "Z" println (BB)//Print: "A...z"
Well, I'm going to go to bed here today.
[Swift] Basic operators in Day01:swift