Swift, additional wrappers. "?" "!" I understand how it works. But why is it better than that! = Nil check

I understand how "!" or "?" works. But I'm not quite sure that the added benefit is compared with validation! = Nil. What is the added benefit of switching to "!?" I feel that this is just something that Apple has added, but cannot really see the added value compared to the iOS status quo. Did I miss something? Thanks in advance.

+5
source share
2 answers

The difference between checking for nil and requiring optional deployment can be the difference between your code crashing or not. When used properly, options can increase security and make your code more readable.

Suppose you have an array and you want to get the first value in it. You can do it as follows:

if !arr.isEmpty { useValue(arr[0]) } 

But, of course, it's easy to forget that the isEmpty part, and if you do, your code will fail with an error outside the bounds.

So this is a much better way: use the first method of the array, which returns an optional parameter with nil if the array is empty:

 if let val = arr.first { useValue(val) } 

With this form you cannot be mistaken. Cannot use arr. first value arr. first arr. first without deploying it. If you forget, you will get a compilation error. Its also more readable to my eyes /

Presumably, you expected your wording != nil to work something like this:

 if arr.first != nil { // all optionals would be "implicit" useValue(arr.first) } 

Putting aside that you call .first twice, which is a bit inefficient, and the question of type compatibility, essentially, this brings you back to the square - you might forget to compare nil and then, kaboom. Or you can take the Objective-C approach and say that nil is suitable for sending messages, but it also leads to all the confusion (personally, I hate the idea of ​​implicitly sending messages to a nil value of no-op), and also leads to the question of what you are doing, when a function returns a value type, such as Int . Should everything be nullable? This leads to a big mess in the land of Obj-C.

Moreover, there are all sorts of amenities that you can enter when dealing with additional features such as nil-coalescing:

 // default to 0 if no first element arr.first ?? 0 // much neater than this equivalent form: arr.first != nil ? arr.first : 0 

or optional comparison:

 // will only be true if arr is non-nil and zero if arr.first == 0 { } 

For more examples see this answer

+5
source

The operator ? very handy when it comes to the chain, for example:

 a?.b?.c?.doSomething() 

Otherwise, you will need to check a , b and c for nil , which can lead to more unreadable code than the chain does.

Another thing is that you can easily mark optional arguments, for example.

 func someFunc(arg: Type?) 

and it immediately becomes clear that this is a type that can be nil , i.e. language provides this. Otherwise, you will pass something that may be nil , and you will forget the check and experience failures.

+5
source

Source: https://habr.com/ru/post/1215305/


All Articles