Swift basics Flashcards

1
Q

How to declare a variable?

A

var hello = “Hello”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to declare a constant?

A

let hello = hello

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to print in the console?

A

println()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to add a new line and a tab in a string?

A
  • New line: \n
  • Tab: \t
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to add an emoji to a string

A
  1. Go to edit
  2. Special characters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to convert an int to double?

A

Double(number)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to add an element to an array?

A
  • array.append()
  • array +=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to get the size of an array?

A

array.count

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how to declare a for loop?

A

1)

for item in array {

}

2)

for item in 1…10 {

}

3)

for var i = 0; i < todo.count; i++ {

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to declare a while loop?

A

var index = 0

while index < todo.count {

println(“hello”)

index++

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to declare a function?

A

func calculate(height: int, width: int) -> Int{

return

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an optional?

A
  • Returns nil or a some object
  • To make a function an optional put ? after the Type
  • if let ss = function() {}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Characteristics of an enum

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a struct?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly