Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift Properties Optional Property

Alexander Certosimo
Alexander Certosimo
1,774 Points

confused with optional property in lesson compared to my xcode

hey all,

So i just did the optional properties lesson and in the lesson when assigning

toy.batteries = true toy.batteries

it shows Some True..but writing the same thing in my xcode it just shows true and not some true. Any reason for this?

Also in the if let statement..why is there a second "if batteries" statement nested in the if let statement? i wrote it without the second if statement and it worked exactly the same?

Just looking to get some clarity. Thank you

Hey, Alexander!

This is an awesome question and I hope I'm able to share any clarity on this from my understanding. :) If you're using Xcode 7+, this might indeed be updates made to Playgrounds in themselves as well as with Swift 2.0. For example, using the same code, the compiler tells me to replace println with just print.

Regarding the if statement there, I think this use of it is super-helpful for when we need to a) have that first line of checking and b) be super-clear on what we want to return. For example:

if let batteries = toy.batteries {
}

We're basically saying, if toy.batteries has a value, store whatever that value is in batteries and then...

if batteries {
    print("Batteries included")
}

...if this new batteries property does have a value, then print this line.

You more than likely already made this distinction between the two so I'm sorry if I'm going over known ground on your part here. :D

Alexander Certosimo
Alexander Certosimo
1,774 Points

Steve!

Thanks so much for the response! I did end up kind of figuring out what was going on, but you taking the time out is much appreciated. I have a similar question maybe you would know better than me. The reason i say it is similar is because i never had this problem before, it just recently popped up. I am getting a warning saying "Initialization of variable 'input' was never used, consider replacing with '_' or removing it". Here is the code.

class ViewController: UIViewController {
      @IBOutlet weak var textInput: UITextView!

      @IBAction func submitText(sender: AnyObject) {
       var input = textInput.text



    }

All i want to do is assign the user inputted text from the UITextView to the variable so i can do some further work with it.. but i am confused as to why i am getting this warning. Been stuck the past couple days

Thanks again!

1 Answer

Stepan Ulyanin
Stepan Ulyanin
11,318 Points

Hey, this is going to be there until you are going to use the variable. If you use the variable further in your code then you need a name and that's where the warning will just go away:

class ViewController: UIViewController {
      @IBOutlet weak var textInput: UITextView!
      @IBAction func submitText(sender: AnyObject) {
             var input = textInput.text
             print(input)       //using the variable will take the warning off the previous line
      }
}

If you are not going to use it then replace the name with _ so you and other programmers would see that this piece of code is not used anywhere further in the code but you might have intentions to

Alexander Certosimo
Alexander Certosimo
1,774 Points

Thanks so much Stepan, this had me confused for days now!