Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

joaogfmoreira

macrumors newbie
Original poster
Feb 24, 2015
24
0
Hello all,

I have noticed something that I can't really understand. Sometimes I created empty variables like this:

Code:
 var manager = CLLocationManager()

And other times I used this:

Code:
 var manager: CLLocationManager!

While writing a program today i noticed that i could only get it to work writing things the first way. Why is that? What's the difference between these two ways?
I'm not quite getting the difference.
 
These are called Optionals. The general idea with the first one is that the value of manager[i/] could be nil, meaning you'd need first check for nil and then unwrap the optional (for the value it contains):

Code:
if (manager != nil) {
   println(manager!)

    /// Do something with manager
}

This is acceptable for many variables as it's common for variables to often be nil at some point. Note that in Objective-C sending messages to objects that are nil won't cause an exception, whereas in Swift they will.


Now you may wish to state that a variable isn't optional, that once it's set it can not be nil. This is where the ! comes into play in the second example. That effectively means that you're guaranteeing the object isn't nil and so the compiler will complain if that object isn't initialised.

Further reading.



Once again thank you very much! Completely understood the difference!
 
I have to disagree with the explanation given. Let's cover some basics first:
The syntax for assigning a value to a variable looks like this:
Code:
var <name> : <type> = <value>
As you can see, in your first statement,
Code:
var manager = CLLocationManager()
you assign whatever value is returend from CLLocationManager() to a variable named manager. The type is not specified (no colon), so Swift uses type inference to determine the type for you. In this case, it looks at CLLocationManager's initializer. Since the initializer is not defined as "failable", it will always return a value, so the type of manager will be inferred as a non-optional. Your statement is equivalent to
Code:
var manager: CLLocationManager = CLLocationManager()
Try assigning nil to manager after this statement and you'll see that the compiler won't let you - proof that it's not an optional.
Now let's look at your second statement:
Code:
var manager: CLLocationManager!
This time, you did explicitly specify a type (CLLocationManager!), but you never assign a value to it. This statement is equivalent to:
Code:
var manager: CLLocationManager! = nil
OP, this is why you never got this to work. No actual CLLocationManager instance is ever created and assigned to your variable.
Finally:
Now you may wish to state that a variable isn't optional, that once it's set it can not be nil. This is where the ! comes into play in the second example. That effectively means that you're guaranteeing the object isn't nil and so the compiler will complain if that object isn't initialised.
That's not true. You specify a non-optional variable without any suffix. Optionals are either declared with a "?" for "regular" optionals or with a "!" for "implicitly unwrapped" optionals, which provide some syntactic sugar, but otherwise behave just like regular optionals. So what Ubuntu said about guaranteeing the object's non-nil-ness is in fact true for your first statement, not for the second one.
 
Last edited:
I have to disagree with the explanation given. Let's cover some basics first:
The syntax for assigning a value to a variable looks like this:
Code:
var <name> : <type> = <value>
As you can see, in your first statement,
Code:
var manager = CLLocationManager()
you assign whatever value is returend from CLLocationManager() to a variable named manager. The type is not specified (no colon), so Swift uses type inference to determine the type for you. In this case, it looks at CLLocationManager's initializer. Since the initializer is not defined as "failable", it will always return a value, so the type of manager will be inferred as a non-optional. Your statement is equivalent to
Code:
var manager: CLLocationManager = CLLocationManager()
Try assigning nil to manager after this statement and you'll see that the compiler won't let you - proof that it's not an optional.
Now let's look at your second statement:
Code:
var manager: CLLocationManager!
This time, you did explicitly specify a type (CLLocationManager!), but you never assign a value to it. This statement is equivalent to:
Code:
var manager: CLLocationManager! = nil
OP, this is why you never got this to work. No actual CLLocationManager instance is ever created and assigned to your variable.
Finally:

That's not true. You specify a non-optional variable without any suffix. Optionals are either declared with a "?" for "regular" optionals or with a "!" for "implicitly unwrapped" optionals, which provide some syntactic sugar, but otherwise behave just like regular optionals. So what Ubuntu said about guaranteeing the object's non-nil-ness is in fact true for your first statement, not for the second one.

Ah, my bad. I'm still rather new to Swift so I should have held off. Thanks for the better answer.
 
I have to disagree with the explanation given. Let's cover some basics first:
The syntax for assigning a value to a variable looks like this:
Code:
var <name> : <type> = <value>
As you can see, in your first statement,
Code:
var manager = CLLocationManager()
you assign whatever value is returend from CLLocationManager() to a variable named manager. The type is not specified (no colon), so Swift uses type inference to determine the type for you. In this case, it looks at CLLocationManager's initializer. Since the initializer is not defined as "failable", it will always return a value, so the type of manager will be inferred as a non-optional. Your statement is equivalent to
Code:
var manager: CLLocationManager = CLLocationManager()
Try assigning nil to manager after this statement and you'll see that the compiler won't let you - proof that it's not an optional.
Now let's look at your second statement:
Code:
var manager: CLLocationManager!
This time, you did explicitly specify a type (CLLocationManager!), but you never assign a value to it. This statement is equivalent to:
Code:
var manager: CLLocationManager! = nil
OP, this is why you never got this to work. No actual CLLocationManager instance is ever created and assigned to your variable.
Finally:

That's not true. You specify a non-optional variable without any suffix. Optionals are either declared with a "?" for "regular" optionals or with a "!" for "implicitly unwrapped" optionals, which provide some syntactic sugar, but otherwise behave just like regular optionals. So what Ubuntu said about guaranteeing the object's non-nil-ness is in fact true for your first statement, not for the second one.

Thank you very much! Understood it completely!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.