Null value merge operator and interval operator
What I mainly look at today is the basic operators in Swift. Record it.
Nil Coalescing Operator
?? in a ?? b is the null value merge operator, which will judge a. If it is not nil, it will unpack it, otherwise it will 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"
There are two requirements when using it:
a must be optional
b must be the same as type a
In other words, a must have the possibility of being a spare tire, and b must be qualified to be a spare tire.
In fact, it is the abbreviation for the three-point operator:
a != nil ? a! : b or a == nil ? b : a!
Of course, you can also implement it through custom operators:
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#. Those who are interested can learn about it.
Range Operator
The interval operators are divided into two types: closed interval (...) and left closed right open interval (..<). The former calculates the beginning and the end, while the latter calculates the beginning and not the end.
Can be applied in switch:
switch aNumber { case 0...5: println("This number is between 0 and 5") case 6...10: println("This number is between 6 and 10") default: println("This number is not between 0 and 10") }
The interval operator actually returns a Range<T> object, a collection of consecutive unrelated sequence indexes.
Speaking of which, the left and right opening was..., which was exactly the same as Ruby's. . .
But some people just want to use .., so they can write one by themselves:
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 = () // {startIndex 1, endIndex 5} () // 1 () // 2 () // 3 () // 4 () // nil
.generate() returns a struct of RangeGenerator<T> that can be used to iterate over the values in Range<T>.
I used to use (5...1).by(-1), but now it seems useless.
The interval operator returns a ClosedInterval or HalfOpenInterval thing, and the type is as long as it is Comparable. So we can also put String in ....
For example, there is a chapter of the code in Cat God's Swifter Tips as follows, using the ClosedInterval of String to output lowercase letters in the string:
let test = "Hello" let interval = "a"..."z" for c in test { if (String(c)) { println("\(c)") } }
SubString
It is very convenient to use dots to obtain SubString in Ruby:
2.1.3 :001 > a="abc" => "abc" 2.1.3 :002 > a[0] => "a" 2.1.3 :003 > a[0..1] => "ab"
The ClosedInterval in Swift does not have subscript. But what should we do if we are willful is using [1...3]?
Do it yourself and make enough food and clothing. Write an extension. Just add a subscript, and then the subscript type is Range<Int>:
extension String { subscript (r: Range<Int>) -> String { get { let startIndex = advance(, ) let endIndex = advance(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 the target string and then intercept the substring, you can do this:
let name = "Joris Kluivers" let start = let end = find(name, " ") if (end != nil) { let firstName = name[start..<end!] } else { // no space found }
The above is the entire content of this article, I hope you like it.