[Swift] Day01: Basic operators in Swift

Source: Internet
Author: User

[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.


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.