[Swift] Basic operators in Day01:swift

Source: Internet
Author: User

The main things to see today are the basic operators in Swift. Record it.

Null-value merge operator (Nil coalescing Operator)

a ?? bIn the??is a null-value merge operator thataBe judged, if not fornilThe 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! : bOra == 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.