Swift basics Flashcards
How to declare a variable?
var hello = “Hello”
How to declare a constant?
let hello = hello
How to print in the console?
println()
How to add a new line and a tab in a string?
- New line: \n
- Tab: \t
How to add an emoji to a string
- Go to edit
- Special characters
How to convert an int to double?
Double(number)
How to add an element to an array?
- array.append()
- array +=
How to get the size of an array?
array.count
how to declare a for loop?
1)
for item in array {
}
2)
for item in 1…10 {
}
3)
for var i = 0; i < todo.count; i++ {
}
How to declare a while loop?
var index = 0
while index < todo.count {
println(“hello”)
index++
}
How to declare a function?
func calculate(height: int, width: int) -> Int{
return
}
What is an optional?
- Returns nil or a some object
- To make a function an optional put ? after the Type
- if let ss = function() {}
Characteristics of an enum
- emum is a type
- It is used for representing days of the week, etc
- The name and the elements of the enum must begin with Capital Letter.
- You can put methods inside an Enum
- enum Days { case Monday, Tuesday}
- enum Days: Int { case Monday = 1, Tuesday = 2}
- Days.Monday.rawValue to obtain the raw value
What is a struct?
- It is a command used to create a data type like Car or Contact
struct Car() {
var speed: Int
var color = “red”
}
var lambo = Car(parameters)
- create an init() to modify the custom initiali