Swift notes operator _swift

Source: Internet
Author: User
Tags generator

Null-value merge operators and interval operators

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

Nil coalescing Operator

A?? b in the?? is a null-value merge operator that will judge a and, if not nil, unpack, or return B.

var a:string? = "a"
var b:string = "B"
var c = A?? b   //"a"
a = nil
c = a?? b     //"B"
B = nil
C = A ?? B?? "C"  //"C"

The following two points are required when used:

A must be optional.
b must be consistent with type a
In other words, a must have the possibility of being spare, B must have the qualification to prepare the tire.

In fact, a shorthand for the three-mesh operator:

Copy Code code as follows:

A!= nil? A! : b or a = = nil? b:a!

Of course, you can also use custom operators to:

Infix operator | | | {} Func | | |

<T> (Left:t, right:t)-> T {
  if let L = left {return 
    L} return right
}

var a:string?
var b = "B"
var c = a | | | b

There is also a in C #?? , interested can go to understand.

Range Operator

The interval operators are divided into closed intervals (...) and left-right open intervals (...). <) Two, the former is to calculate the end of the head, the latter is not the end of the calculation.

can be applied in switch:

Switch anumber
{case
0...5:
  println (' This # is between 0 and 5 ') Case
6...10:
  println (" This is between 6 and ten ")
Default: println (" This number is not
  between 0 ")
}

The interval operator actually returns a Range<t> object and is a collection of sequential indexes with no associated sequence.

Say, left and right. , which is exactly the opposite of Ruby's ...

But someone just wants to use. , 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...4
var generator = range.generate ()  //{StartIndex 1, endindex 5}
Generator.next ()//1
Generator.next ()//2
Generator.next ()//3
Generator.next ()//4
Generator.next ()//Nil

. Generate () returns a rangegenerator<t> structure that can be used to traverse the values in range<t>.

There used to be a (5...1). by (-1) usage, but it seems useless now.

The interval operator returns a closedinterval or Halfopeninterval thing, and the type is just comparable. So we can also put String into ... In

For example, Cat God's swifter tips in a chapter of the code below, through a string of closedinterval to output the lowercase letters in the string:

Let test = "Hello" let
interval = "a" ... "Z" for

C in test {
  if Interval.contains (String (c)) {
    println ("\" C )")
  }
}

SubString

Ruby is handy for using dots to get SubString:

2.1.3:001 > a= "abc"
 => "abc"
2.1.3:002 > A[0]
 => "a"
2.1.3:003 > a[0..1]
 => "AB"

And there is no subscript in Swift's closedinterval. But the wayward we are to use [1...3] This method how to do?
own hands and clothing, write a extension bar, only need to add a subscript on it, and then the type of subscript is range<int> can be:

Extension string {
  subscript (r:range<int>)-> string {Get
    {let
      startIndex = Advance (self.startin Dex, R.startindex) let
      endindex = Advance (StartIndex, R.endindex-r.startindex) return

      self[range (start: StartIndex, End:endindex)]}}

var s = "Hello, playground"

println (s[0...5])//==> "Hello,"
println (s[0..<5])//==> "Hello"

If you want to search for a target string and then intercept substring, you can do this:

Let-name = "Joris kluivers" let

-start = Name.startindex let
-end = Find (name, "")

if (end!= nil) {
  Le T firstName = Name[start. <end!]
} else {
  //No space found
}

The above mentioned is the entire content of this article, I hope you can enjoy.

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.