-1

I am trying to write in swift something that should be very basic, but I can't seem to get a handle on it :

First, I create a global variable. For example:

var xx:Int

Then, I want to create a conditional instruction. Something like :

if (xx == 1){
//do something
}
else if (xx == 2) {
//do something else
}

I can do this very easily in Objective-C, but I can't seem to be able to do it in Swift. I have looked everywhere, and don't seem to find the answer.

Glyphs
  • 97
  • 1
  • 1
  • 7

3 Answers3

1

With the code you provided you're probably getting the error: "Variable xx used before initialized". This is happening because the declaration of the variable is incomplete, you neither gave a value to the variable nor told the compiler it is an optional. You have three options:

  • Give a initial value to it; var xx: Int = //value here
  • Declare it as an optional (doing this you say that it may not have a value, if it does the code will be executed, if it doesn't it won't); var xx: Int?
  • Force unwrap the variable (it still an optional, but if you force-unwrap it you're assuring the compiler that the variable will have a value when needed, otherwise it'll crash); var xx: Int!
  • Both ! and ? seem to work (and "var xx:Int" seemed to work too), but when I then write "xx = 2" or "if xx == 1 {}" (in viewDidLoad for this example), I get the message "Use of unresolved identifier xx" – Glyphs Jul 03 '15 at 07:35
0

Or you can say var xx = Int() that way it's initialized and the default initialization is equal to 0. This is different than the other answers and allows you to have a value from the get go if you're not sure what value might be assigned during runtime.

pbush25
  • 5,228
  • 2
  • 26
  • 35
  • I tried, but I get the following message: "Consecutive declarations on a line must be separated by ';' " It seems I should put a ';' after Int – Glyphs Jul 03 '15 at 07:32
  • Based on all the things you've said on this post, it almost sounds like you're trying to use Swift in an Objective-C file and the compiler is confused. – pbush25 Jul 05 '15 at 19:36
0

In addition to the other poster's point that you must assign an initial value before you can use xx, you also need to lose the parentheses around the condition in your if statement:

var xx:Int

xx = 2

if xx == 1
{
  //do something
}
else if xx == 2 
{
  //do something else
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • He doesn't have to lose the parentheses, that's syntactic sugar in Swift. It will work with or without them. – pbush25 Jul 03 '15 at 00:38
  • That's a matter of personal preference. – Gabriel Bitencourt Jul 03 '15 at 01:26
  • I tried, but when I then write "xx = 2" or "if xx == 1 {}" (in viewDidLoad for this example), I get the message "Use of unresolved identifier xx" – Glyphs Jul 03 '15 at 07:36
  • You're going to have to show more background then. Where are you declaring xx? If you want it to be an instance variable of your class you need to define it inside the outermost braces of that class, but not inside a method. If you put a variable inside a method, it is only defined in the scope of that method. – Duncan C Jul 03 '15 at 11:02
  • Yes, I put it outside of the class, like I did when I worked in Objective-C. Putting it inside of the class seems to solve the problem. I'll try to make some tests with your solution. – Glyphs Jul 03 '15 at 12:45