Fengson
Fengson

Reputation: 4912

Using var to unwrap an optional in Swift?

I've only seen people using let to unwrap optionals.
I was wondering if it is a good practice and I could just as well use var, or is there something more to it?

What are the pros and cons of using let versus var?

Upvotes: 0

Views: 252

Answers (2)

user4592375
user4592375

Reputation:

As mentioned before, you should always use let when you can, as it allows compiler do some fancy stuff and thus makes your program run faster.

There is technically nothing wrong with using var, though.

For further reading, please check out this article:
http://www.touch-code-magazine.com/swift-optionals-use-let/

Upvotes: 1

LastMove
LastMove

Reputation: 2482

It like usual; let is for a constant (you will not be able to modify the value). The good practice is to always use let when you can (when you don't need to modify the value). It is for optimization purpose and for clarity of the code.

Upvotes: 4

Related Questions