What declaration is used in quick

I really don't know how to declare my variables in Swift, I have four options:

var value = 0.0 // I know this one declares value as a Double with the number 0.0 var value: Float = 0.0 // This one declares value as a Float with the number 0.0 var value: Float? // I really don't get the difference between this var value: Float! // and this, I just know that both of them are used to declare a variable without a value, am I right ? 

I just want to declare a float without value attribution, what is the best way?

+5
source share
2 answers

Float? is optional. Float! implicitly unfolds optionally. For details on the latter, see the “Implicitly Disabled Options” section of Swift Programming: The Basics , which says:

[Options] indicate that the constant or variable is set to "no value". Options can be checked using the if statement to find out if a value exists and can be conditionally expanded with an optional binding to access the value of the options, if it exists.

Sometimes it is clear from the structure of programs that the optional will always matter after that value is set first. In these cases, it is useful to remove the need to check and expand the value of options each time it is accessed, because we can safely assume that it matters all the time.

These types of options are defined as implicitly expanded optional. You write an implicitly expanded option by placing an exclamation mark (String!), Rather than a question mark (String?) After the type you want to make optional.

Implicitly expanded options are useful when it is confirmed that the value of optionals exists immediately after it is optionally defined first and, of course, it can presumably exist at every point after that. Initial use of implicitly deployed options in Swift during class initialization, as described in Unowned References and Implicitly Unwrapped Additional Properties.

An implicitly expanded optional parameter is optional by default, but can also be used as an optional value without having to expand the optional value each time it is accessed.

You describe that value will be a property of the class. If it was a Float that you always set in the init method, you won’t need to make it optional at all, so you can use your second syntax. If it is a variable, which may be nil or not nil at any point, then you will make it optional (your third syntax). But if it is a variable that you cannot set in init , but as soon as you set it, it will always matter from that point, you can lean towards an implicitly expanded optional (your fourth syntax).

+3
source

You have many more options! Above my head you can also:

 var value = Float(0) lazy var value: Float = 1 var value = {0 + 0 as Float}() 

Etc etc. In any case: I assume that these are, for example, variables; This is not clear from your question.

First, let's talk about options: you should (if possible) avoid options, unless you are dealing with a situation where the value may be clearly absent; for example, a view that you sometimes create, or a delegate that is not always set. In my opinion (and, according to smart people, then I, see the Github Swift Style Guide ), you should use the implicitly expanded options ( value: Float! ) As little as possible. We often see them in the Apple APIs because these APIs are accepted from frameworks that were written in Obj-C, which uses nil in different ways. The only time that something needs to be implicitly expanded is optional, when it can only be installed after init , and when it will never be zero; the place that we see most often is in the views loaded from the storyboard, as this happens immediately after the view controller is initialized.

For other templates: if you do not want to set the initial value, I think that initializing to zero is a good, clear template. This is certainly better than using optional if you are not very specific about reporting that this value may not matter at some point in the future.

If you are dealing with something heavier, and then with a floating point (say, an object that is expensive to create an instance), I prefer the lazy template, because if you never access the value before it is assigned, it will never be created. There are currently several compilers on this issue, so at this point it is preferable to use options; Anyway, lazy uses options backstage.

+1
source

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


All Articles