Swift’s syntax is secure and robust, but also has many very flexible features, such as?? Operators are one of them. For those of you who already know it, and for those of you who don’t know it, here’s a list of interesting discussions about this operator.

?? Operator description

Before we go any further, let’s understand what this operator does. This operator is related to Optional. Let’s look at an example:

var a:Int? print(a ?? 2) / / 2Copy the code

?? It has an Optional value on the left and a normal value on the right, and what it does is, if the Optional value on the left is nil, it returns the normal value on the right, and if the Optional value on the left is not nil, Returns the unpacked value of Optional on the left.

For example, in our example, the value of variable A is nil, so the print statement will print the default value 2.

In that example, if you don’t use…? Operator, then this is the logic:

print(a == nil ? 2 : a) 
 Copy the code

Let’s see…? Operator definition:

func ?? (optional: T? , @autoclosure defaultValue: () throws -> T) -> TCopy the code

To simplify it, this is:

func ?? (optional: T? , defaultValue: () throws -> T) -> TCopy the code

It accepts an Optional value of type T, a plain value of type T as the default, and returns an unpacked value of type T. The final return value is determined by whether the first argument is null.

?? Some discussion of operators

?? The operator makes it easier to handle Optional. But there are a few hiccups, as discussed in Swift’s official mailing group. Take this code for example:

func test(x: ((String) -> String)? = nil) {

    let qf = { (p:String) -> String in return "tt" }

    let fn = x ?? qf
    fn("tt")

}Copy the code

The test function takes another parameter of type Optional, and inside it calls?? The operator determines whether the function x passed in is nil, and if so, replaces it with another function inside, qf.

At first glance, such code should be fine,?? Is the variable type to the left of ((String) -> String)? And the variable on the right is of type (String) -> String. Operator definition.

But this paragraph is right about…? The use of the operator gave an error at actual compile time. We must declare qf as Optional to use:

let qf:((String) -> String)? = { (p:String) -> String in return "tt" }
    let fn = (x ?? qf)!
    fn("tt")Copy the code

This compiles, but does not comply with?? The operator is defined. This time both sides are Optional, and the return value of the expression is Optional.

At this time about?? In a recent discussion in the Swift email group, the last reply stated that this was a Swift bug that will be fixed in Swift 3.0.

?? Operators can have different types

In the mail group, there is another discussion, you can take a look at this code:

var a:Int?
print(a ?? "test")
Copy the code

Notice that the a on the left is an Int, right? Type, and the right-hand side is a String. This code compiles with the current Swift 2.2 build.

This topic has been discussed quite a bit, but it makes sense that the Swift compiler will transition up accordingly depending on the scenario in which the current expression is being expressed.

For example, as arguments to print, the compiler will treat them as Any, because print takes Any arguments. So this statement will compile.

If this statement is used in this scenario, the compilation will fail:

let x = a ?? "test"
Copy the code

This time, the scenario is to assign its return value to x. This time, however, the compiler could not determine their common type and instead treated them as two different types, which caused the compilation to fail.

Again, this code will compile:

let x = a ?? NSDate()
Copy the code

And by that logic, because the next type is NSDate they can all be subclasses of NSObject, so the compiler treats them all as generic types of NSObject.

Let x = a?? “Test” because the strings that follow are of type String in Swift, the compiler cannot find their generic parent and fails.

Of course, this is the current discussion and analysis results, you can also think together, tell your analysis and communicate with everyone.

conclusion

This time we discussed together… The role of the operator, and some of the points to note in its use. Here you can see a lot of detail about the language itself. The Swift mailing group is a great resource for in-depth discussions on a wide range of topics and is completely open to request access on its home page:

Lists.swift.org/mailman/lis…

This site articles are original content, if you need to reprint, please indicate the source, thank you.

If you want to read more exciting content, please follow the wechat public account Swift-cafe and send it to you as soon as possible



We recommend following the wechat public platform



swift-cafe