Creating variables

Discussion in 'iOS Programming' started by joaogfmoreira, Mar 13, 2015.

  1. joaogfmoreira macrumors newbie

    Joined:
    Feb 24, 2015
    #1
    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.
     
  2. Ubuntu, Mar 13, 2015
    Last edited: Mar 16, 2015

    Ubuntu macrumors 68000

    Ubuntu

    Joined:
    Jul 3, 2005
    Location:
    UK/US
    #2
    EDIT: I'd recommend looking at Cipo's answer instead.
     
  3. joaogfmoreira thread starter macrumors newbie

    Joined:
    Feb 24, 2015
    #3



    Once again thank you very much! Completely understood the difference!
     
  4. cipo, Mar 16, 2015
    Last edited: Mar 16, 2015

    cipo macrumors member

    Joined:
    Nov 23, 2010
    Location:
    Stuttgart, Germany
    #4
    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.
     
  5. Ubuntu macrumors 68000

    Ubuntu

    Joined:
    Jul 3, 2005
    Location:
    UK/US
    #5
    Ah, my bad. I'm still rather new to Swift so I should have held off. Thanks for the better answer.
     
  6. joaogfmoreira thread starter macrumors newbie

    Joined:
    Feb 24, 2015
    #6
    Thank you very much! Understood it completely!
     

Share This Page