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

Megan Garcia
Megan Garcia
1,712 Points

How to Initialize a variable called myBool that contains a Boolean.

How do I initialize a variable called myBool that contains a Boolean? I thought it was var myBool: Bool = true

but I keep getting an error. Help?

Jhoan Arango
Jhoan Arango
14,575 Points

Hello,

What kind of error are you getting ?

Try :

var myBool = true
Megan Garcia
Megan Garcia
1,712 Points

I'm using real.it for school. When I use the code below you provided it states I need to declare a variable. I've tried using:

var myBool = Bool = true var myBool = true var myBool = myBool = true.

I suppose it could just be an error within the program I'm using? I just want to make sure I'm on the right path as far as what my code looks like for this.

Jhoan Arango
Jhoan Arango
14,575 Points

It could be an error with the program, because I do not see any other ways of declaring a variable that contains a bool..

3 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Megan,

Is what you've posted the whole code of the file that gave you the error? I'm assuming you're using [repl.it](repl.it) and the name got mangled by autocorrect. I've tried your exact code on repl.it and it works fine, so if you're getting an error with that exact code chances are you have an error somewhere else in your code (probably a line or two previously) and the compiler is only spotting the error once it gets to the line you've described.

For what it is worth, the code you provided in your question is a perfectly valid way to declare a variable and explicitly declaring the type. Generally, it is considered better style in Swift when you are declaring a variable with an unambiguous type, like true to let the compiler infer the type (which would be the code that Jhoan suggested). However, this is purely a stylistic difference and var myBool: Bool = true is absolutely valid code in all versions of Swift.

Cheers

Alex

Megan Garcia
Megan Garcia
1,712 Points

Hey Alex,

There are more lines of code. Below is what is being asked to do:

These variables can have any value as long as their type is correct as directed below:

Initialize a variable called myInt that contains an Int. Initialize a variable called myString that contains a String. Initialize a variable called myBool that contains a Boolean.

This is what I have in my actual code for these: var myInt: Int = 0 var myString: String = "Hey" var myBool: Bool = true

in the program though it doesn't have "Bool" listed as a keyword, it only has "bool." I'm not sure if that's just an error or if I'm still missing something.

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Megan,

I copied and pasted your code into a repl.it instance. It works fine, as you can see for yourself. The only thing I can think of is that maybe what you have in your repl is slightly different to what you typed above. If your repl is still giving errors, you could post a link to it and we could look at that code exactly.

As for your point about repl.it having bool listed as a keyword, I see that too (although I also see 'Bool' listed). I think that might be a bug in repl.it: bool is not, as far as I could tell, a reserved word in Swift, and all types in Swift have capital first letters.

Cheers

Alex