Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which declaration to use in swift [duplicate]

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 ?

like image 216
Drakalex Avatar asked Dec 15 '14 22:12

Drakalex


People also ask

How do you declare in Swift?

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.

What is a Swift declaration?

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.

What is the difference between LET and VAR in Swift?

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.

What is an import declaration in Swift?

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.

How to declare an array in 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.

What is the use of declaration in C programming?

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.

How does the Swift compiler handle multiple symbols with the same name?

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:


1 Answers

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).

like image 89
Rob Avatar answered Oct 14 '22 22:10

Rob