Swift basics - the very basics (variables, constants, types)
To open the debug area:
shift+command+Y
Add images to swift playground:
1. command + 1
2. Drop image over to resources folder
3. Create an object with the image:
var image = UIImage(named: "sc.png")
Using the for loop:
var j = 1
for i in 1...5
{
j += i
}
for in loop: used with collection or range of numbers:
for index in 1...5
{
print(index)
}
for item in firstArray{}
for (index, value) in dict1{}
while x<7
{
x = Int(arc4random_uniform(10))
}
ar4random above gives a random number between 0 and 9
Instead of do while, swift makes use of repeat while
var x: Int
repeat
{
x = Int(arc4random_uniform(10))
} while x < 4
Add comments:
//
/* */
///
The triple slash is for documenting code
For example:
To document a function:
When you press the option key and click on the function name, information on the function as above will appear.
Creating a function
func adding(nameOfFirst: Int, secondParameter: Int) -> Int
{
let sum: Int = nameOfFirst + secondParameter
return sum
}
adding(nameOfFirst: 4, secondParameter: 6)
Use of semicolons: Can be used but unnecessary
If else:
if i == 1
{
}
While statement:
while i == 1
{
}
Printing a line inside playground:
print("Hello world")
Add a variable to a string:
var day = "Wednesday"
var msg1 = "Today is \(day)"
Print 3 variables separated by a comma:
print(day1, day2, day3, separator:", ", terminator:"")
Direct the output to a variable:
var nameOfVar = ""
print(day1, day2, day3, separator:", ", terminator:"", to:&nameOfVar)
shift+command+Y
Add images to swift playground:
1. command + 1
2. Drop image over to resources folder
3. Create an object with the image:
var image = UIImage(named: "sc.png")
Using the for loop:
var j = 1
for i in 1...5
{
j += i
}
for in loop: used with collection or range of numbers:
for index in 1...5
{
print(index)
}
for item in firstArray{}
for (index, value) in dict1{}
while x<7
{
x = Int(arc4random_uniform(10))
}
ar4random above gives a random number between 0 and 9
Instead of do while, swift makes use of repeat while
var x: Int
repeat
{
x = Int(arc4random_uniform(10))
} while x < 4
Add comments:
//
/* */
///
The triple slash is for documenting code
For example:
To document a function:
///
///The adding function will the take 2 parameters
/// - parameter nameOfFirst: First number to add
/// - parameter secondParameter: Second number to add
/// - returns: the sum of the two numbers
/// - throws: any error
///
Creating a function
func adding(nameOfFirst: Int, secondParameter: Int) -> Int
{
let sum: Int = nameOfFirst + secondParameter
return sum
}
adding(nameOfFirst: 4, secondParameter: 6)
Use of semicolons: Can be used but unnecessary
If else:
if i == 1
{
}
While statement:
while i == 1
{
}
Printing a line inside playground:
print("Hello world")
Add a variable to a string:
var day = "Wednesday"
var msg1 = "Today is \(day)"
Print 3 variables separated by a comma:
print(day1, day2, day3, separator:", ", terminator:"")
Direct the output to a variable:
var nameOfVar = ""
print(day1, day2, day3, separator:", ", terminator:"", to:&nameOfVar)
Naming rules for variables:
Must not contain whitespace.
Must not contain mathematical symbols or arrows.
Must not have private-use or invalid unicode characters.
Must not start with a number, but can contain numbers.
Don't use Swift keywords as identifiers, like class.
Define a constant which will not change once a value is assigned to it:
let valueOfPi = 3.142
Define a variable
var numberOfGuests = 50
Declaring multiple constants:
let valueOfPi = 3.142, speedOfGravity = 10
Declaring multiple variables:
var num1 = 1, num2 = 2
Variables are defined by their initial values.
To indicate the type explicitly:
var valueOfPi: Float = 3.14
//so the above won't be turned into a double
Here are the different types of integers:
Int8
Int16
Int32
Int
UInt8
UInt16
UInt32
UInt64
UInt
To get the minimum or maximum possible values of each type:
UInt8.max
UInt16.min
To print it out:
print("\(UInt8.max)")
You can also insert arbitrary underscores in numbers:
let speedOfLight = 300_000
The underscores are ignored
Difference between float and double:
Float is used to represent a 32 bit floating-point number.
Double is used to represent a 64 bit floating-point number.
Float80 represents an 80 bit floating-point number.
How to add an Integer and a Double:
var a: Int = 1
var b: Double = 0.14
var c = Double(a) + b
var d = 32
var e = Float(d)
var f = UInt16(d)
Using boolean variables in if else code:
var abc = true
if abc
{
}
Operators
assignment
let x = 1
Comparison:
==
!=
>
<
>=
<=
Arithmetic
+ - * /
Remainder
var x = 20 % 6
Compound assignment
+=
-=
*=
/=
Ternary operator:
When you assign a value depending on whether a criteria is met:
var a = 1
var b = 2
var c = (b>a ? "B is greater" : "A is greater")
Logical operators:
NOT
a = !b
(if b is true, a is false)
AND
var a = true
var b = false
var c = a && b
//(false)
OR
var a = true
var b = false
var c = a || b
//(false)
Comments
Post a Comment