[Swift] Constants and Variables

0
47

Swift Programming


-Contents-

  1. Declaring Constants and Variables
  2. Type Annotations
  3. Naming Constants and Variables
  4. Printing Constants and Variables
  5. Comments
  6. Semicolon
  7. Integer
  8. Floating-Point Numbers

Declaring Constants and Variables

In Swift, there are two constant and variable below
let : constant
var : variable
Unlike many other programming languages, Swift language is a Type Free(swift can find its type automatically even if we don’t explicitly declare it) so “without declaring types” doesn’t mean that it’s an error. I gave 10 and 0 to each constant and variable so maximumNumberOfLoginAttempts and currentLoginAttempt will be Integer Type constant and variable.

let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0

You can also declare more variables in one line like below.
The thing : Int? next to the variable and constant is Optional Value. The reason why I put Optional Value here is that I want to leave it without giving values to them. Basically, we have to give initial value to constant and variable which means we mandatorily need a value in order to bring constant and variables 

let constant1 = 0, constant2: Int?
var variable1 = 0, variable2: Int?


Type Annotations

Like above, we annotated the types with colon and the name of Type.

var welcomeMessage: String
var number: Int
var green, blue, red: Double
var doubleType = 0.0 //with Decimal number, Double
var stringType = “hi” //more than one character, String
var a = “a” //a character, Character

**print(a is Character) //is operator – You can also check if the type Swift correctly understood with is operator


Naming Constants and Variables

Surprisingly, constant and variable names can have almost any character, including Unicode character.
But, whitespace, mathematical symbols, arrows, private-use, Unicode code points, -line, box-drawing and name beginning with number are not allowed.

//available
let π = 3.14159
let 你好 = “你好世界”
let 🐶🐮 = “dogcow”

//not available
let 2var = 1.2
let 2+1 = 1.2
let 23 = 1.2


Printing Constants and Variables

It’s how to print on console window. The print(_:seperator:terminator)function is a global function that prints one or more values to an appropriate output. The separator and terminator parameters in the print function have a default value so you can use this function without using parameters.

print(“Bonjour!”)// Prints “Bonjour!
print(“One two three four five”) // Prints “One two three four five”
print(15) // Prints “1…5”
print(1.0, 2.0, 3.0, 4.0, 5.0) // Prints “1.0 2.0 3.0 4.0 5.0”

The print function is basically having ‘\n’ which terminates the line. So, if you don’t want to break the line, use the terminator parameter like below.

for n in 15 { print(n, terminator: “”) } // Prints “12345”

And also, if you want to add spaces or repetitive character between the outputs that you want to print, use the separator parameter like below.

print(1.0, 2.0, 3.0, 4.0, 5.0, separator: ” … “// Prints “1.0 … 2.0 … 3.0 … 4.0 … 5.0”

You can refer to the official API to get more information about print function.

https://developer.apple.com/reference/swift/1541053-print


Comments

To add comments is very simple.
Single-line comments begin with two forward-slashes(//)
Multiline comments start with a forward-slash followed by an asterisk(/*) and end with an asterisk followed by a forward-slash(*/).

// This is a comment.

/* This is also a comment
but is written over multiple lines. */

/* This is the start of the first multiline comment.
/* This is the second, nested multiline comment. */
This is the end of the first multiline comment. */


Semicolon

Unlike many other languages, Swift does not require you to write a semicolon (;) after each statement in your code, although you can do so if you wish. However, semicolons are required if you want to write multiple separate statements on a single line

let cat = “cat”; print(cat)
// Prints “cat”


Integer

Integer is the number without fractional component such as 21, -21. Integers are signed(+,0,-) and unsigned(+,0).
swift provides 8, 16, 32, 64-bit forms. unsigned 8-bit Integer is UInt8 and signed 32-bit Integer is Int32.

You can find the boundary of Min and Max by .min and .max function of Integer.

let minValue = UInt8.min // minValue is equal to 0, and is of type UInt8
let maxValue = UInt8.max // maxValue is equal to 255, and is of type UInt8

Floating-Point Numbers

Floating-point numbers are with fractional component such as 3.14, -213.2.
Double is 64-bit floating-point number,
Float is 32-bit floating-point number.

**Double has a precision of at least 15 decimal digits, whereas the precision of Float can be as little as 6 decimal digits. You should use these two type depending on the nature and range of values you need to work with in your code.

LEAVE A REPLY