Some basic information about Swift variables. Nothing to complicated if you programmed before, no matter what language you used. Variables are variables in any language. Anyway, some basic stuff about Swift variables.
Integers are whole numbers. Like 1 or 5 or even 1000. Integers can also of course be a negative number like -54. Unless you are working with unsigned integers. Those are positive integers only.
How to declare a integer in Swift?
var myLuckyNumber = 7
Or you can declare it like this:
var myOtherLuckyNumber: Int = 77
This last method is not really needed as long as you use a number. Swift will know what kind of datatype you are declaring. If however you want to declare an integer without assigning a value to it. You will need to specify the datatype, like integer. You use the second method just with out the equal sign and number. Like this:
var myNewInteger: Int
And if you want your integer to be a constant. Not being able to change its value, you replace the var keyword with let. Example:
let myIntegerConstant = 3.14
Doubles are numbers with decimals. Like pi (3.14) is a Double. And 5.64 just to give a few examples. -104.5 is also a Double.
How to declare a Double in Swift?
var myUnluckyNumber = 18.98
Or you can specify double, like this:
var myDoubleNumber:Double = 56.999
Again, it is not needed to specify double as long as you use the decimal number in the declaration.
Booleans (or bool) are either true or false. They can only hold true or false.
Example bool constant:
let iAmSoHappy = true
or like this:
var restartGame:Bool = false
They are useful for “flags” to trigger events in your programs. Like in the example above. To tell your game to restart or when it is game over, level completed and things like that.
String is another swift variables, and in any other programming language I would guess. Strings are text. Just text or characters really.
Example on how to declare a string variable in swift:
var myString = “This is a typical string”
or the second method:
var myString:String = “This is another string”
They can also be constant, like all other datatypes.
This is just a few examples on swift variables. There are more datatypes, which you can find in the Apple documentation. Just click the link there.
Thats it for now.