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 attributing a value, what is the best way ?
The var keyword is the only way to declare a variable in Swift. The most common and concise use of the var keyword is to declare a variable and assign a value to it. Remember that we don't end this line of code with a semicolon.
A declaration introduces a new name or construct into your program. For example, you use declarations to introduce functions and methods, to introduce variables and constants, and to define enumeration, structure, class, and protocol types.
let is used to declare an immutable constant. You cannot change the value of it later. var is used to create a mutable variable that you can change later.
In this way, import declarations are the glue that holds everything together. Yet despite their importance, most Swift developers are familiar only with their most basic form: This week on NSHipster, we’ll explore the other shapes of this most prominent part of Swift.
Declaring an array in Swift is quite easy, and it then allows all the elements and values to get accessed and manipulated easily. There are two ways to declare an array which are as follows: One way is to initialize the variable with an empty array. Another way is to use the automatic type inference.
For example, you use declarations to introduce functions and methods, to introduce variables and constants, and to define enumeration, structure, class, and protocol types. You can also use a declaration to extend the behavior of an existing named type and to import symbols into your program that are declared elsewhere.
For example, the following import declaration adds only the swim () function from the Pentathlon module: When multiple symbols are referenced by the same name in code, the Swift compiler resolves this reference by consulting the following, in order:
The Float?
is an optional. The Float!
is an implicitly unwrapped optional. For details on the latter, see the Implicitly Unwrapped Optionals section of The Swift Programming Language: The Basics, which says:
[Optionals] indicate that a constant or variable is allowed to have “no value”. Optionals can be checked with an if statement to see if a value exists, and can be conditionally unwrapped with optional binding to access the optional’s value if it does exist.
Sometimes it is clear from a program’s structure that an optional will always have a value, after that value is first set. In these cases, it is useful to remove the need to check and unwrap the optional’s value every time it is accessed, because it can be safely assumed to have a value all of the time.
These kinds of optionals are defined as implicitly unwrapped optionals. You write an implicitly unwrapped optional by placing an exclamation mark (String!) rather than a question mark (String?) after the type that you want to make optional.
Implicitly unwrapped optionals are useful when an optional’s value is confirmed to exist immediately after the optional is first defined and can definitely be assumed to exist at every point thereafter. The primary use of implicitly unwrapped optionals in Swift is during class initialization, as described in Unowned References and Implicitly Unwrapped Optional Properties.
An implicitly unwrapped optional is a normal optional behind the scenes, but can also be used like a nonoptional value, without the need to unwrap the optional value each time it is accessed.
You describe that value
will be a class property. If this was a Float
that you're always setting in the init
method, then you wouldn't need to make it an optional at all, so you might use your second syntax. If it's a variable that might be nil
or non-nil
at any point, then you'd make it an optional (your third syntax). But if it's a variable that you cannot set in init
, but once you set it, it would generally always have a value from that point on, might you lean towards the implicitly unwrapped optional (your fourth syntax).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With