Swift combines the best of:
C and Objective-C.
Print the value of a variable inside a text
place it in parentheses, and insert a backslash just prior to the opening parenthesis:
print("The value is \(myVariable)")
For printing only the variable value:
The keyword to declare a variable
The keyword var
The following example declares a variable, "a", and assigns its value as "42".
var a = 42
It is possible to change variable values over time:
a = 88
// Now "a" has a value of 88.
Declare a constant
using the let keyword.
This example declares a constant named "one" and assigns it a value of 1:
let one = 1
Declare multiple constants on a single line
separate them with commas:
let x = 0.0, y = 0.0, z = 0.0
Can constant and variable names contain almost any character?
Constant and variable names can contain almost any character, including Unicode characters.
However, constant and variable names must have no blank spaces, mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line and box-drawing characters. Numbers can appear anywhere within a name, except for at the beginning.
Constants or variables of a certain type can't be declared again with the same name, nor can they be altered to store values of differing types. Also, a constant cannot be made a variable, nor can a variable be made a constant.
Type annotations ensure that your code is clear about the value stored within your constant or variable. Swift's basic types include:
Double and Float: Floating-Point Values
Bool: Boolean Values
String: Textual Data.
Add a type annotation by placing a colon (:) after the constant or variable name, then add a space, and then add the type name, as follows:var welcomeMsg: String
welcomeMsg = "Hello"
Are type annotations necessary?
In practice, you will rarely need to add type annotations. Providing an initial value for a constantor a variable at the point at which it is defined, will almost always be sufficient for Swift to infer which type should be used:
let pi = 3.14159 //Double
An operator is a special symbol or phrase used to check, change, or combine values.
Has a single target (-a). A unary prefix operator is placed before the target (!b). A unary postfix operator is placed after the target (i++).
Has two targets (4 + 5) and is infixed, appearing between the two targets.
Has three targets. Like C, Swift has one ternary operator, the ternary conditional operator (a ? b : c).
assignment operator (a = b) initializes or updates the value of a with the value of b:
let b = 7
var a = 42
a = b // a is now equal to 7
The addition operator is also supported for
"Hello, " + "world" // equals "Hello, world"
The remainder operator (a % b) calculates the number of multiples of b that fit within a, and returns the value that is left over, or the remainder.
9 % 4 // equals 1
Unlike the remainder operator in C and Objective-C, Swift's remainder operator also operates on floating-point numbers:
8 % 2.5 // equals 0.5
Increment and Decrement Operators
Swift's increment operator (++) and decrement operator (--) are used as shortcuts that increase or decrease the value of a numeric variable by 1.
var i = 0
++i // i now equals 1
If the operator appears before the variable, it increments the variable before returning its value.
If the operator appears after the variable, it increments the variable after returning its value.
Like C, Swift provides compound assignment operators that combine assignment (=) with another operation.
var a = 1
a += 2
// a is now equal to 3
Swift supports all of the standard comparison operators in C:
Equal to (a == b)
Not equal to (a != b)
Greater than (a > b)
Less than (a < b)
Greater than or equal to (a >= b)
Less than or equal to (a <= b)
Each of the comparison operators returns a Bool value indicating whether or not the statement is true
Ternary Conditional Operator
The ternary conditional operator is a special operator with three parts, taking the form (question ? answer1 : answer2).
This operator acts as a shortcut in evaluating one of two expressions, based on whether the question is true or false. For a true question, it evaluates answer1 and returns its value; otherwise, it evaluates answer2 and returns its value.gender == 0 ? print("male") : print("female")
The example above will evaluate the expression gender == 0. If true, it prints "male". Otherwise, it prints "female".
Swift offers two range operators, which are shortcuts for expressing a range of values.
The closed range operator (a...b) defines a range running from a to b, and includes the values a and b. The value of a must not be greater than that of b.
1...3 //1, 2, 3
The half-open range operator (a..defines a range that runs from a to b, but does not include b. As with the closed range operator, the value of a must not be greater than that of b.
1..<3 //1, 2
Logical operators modify or combine the Boolean logic values true and false. Swift supports the three standard logical operators found in C-based languages:
Logical NOT operator (!a): Inverts a Boolean value so that true becomes false and false becomes true.
Logical AND operator (a && b): Creates logical expressions in which both values must be true for the overall expression to be true.
Logical OR operator (a || b): An infixed operator made from two adjacent pipe characters. It creates logical expressions in which only one of the two values has to be true for the overall expression to be true.
Optionals are used in situations in which a value may be absent.
An optional says:
-There is a value, and it equals x
-There isn't a value at all
var myCode: Int? = 404
An optional Int is written as Int?, not Int. The question mark indicates that the value contained within is optional, meaning that it might contain some Int value, or it might contain no value at all.
You set an optional variable to a valueless state by assigning it the special value...
You set an optional variable to a valueless state by assigning it the special value nil:
var myCode: Int? = 404
myCode = nil
// myCode now contains no value
An optional variable with no default value is automatically set to nil for you:
var someMsg: String?
// someMsg is automatically set to nil
nil cannot be used with non-optional constants and variables. If your code contains a constant or variable that needs to work with the absence of a value under certain conditions, always declare it as an optional value of the appropriate type.