Beginner Swift Flashcards
Property of an array that counts the number of items in a given array.
Var.count
What is an array?
A sequential list of variables. Always starts at 0, increments by one in order.
Var todo[string] = [“call”,”suck”]
To add a value on end:
todo += [“swallow”]
A single operator that can affect the resulting value of a variable.
Unary operators
++ increment
– decrement
- negative
! Means variable or constant is negated
Example of a function with a return type String..
func heyName(name: String) -> String { var greeting = "Hello \(name)" return greeting }
Allows you to create a new type that groups several related values.
Enum.
enum Coin {
case Penny, Nickel, Dime, Quarter
}
Penny etc are called “Members”
What is a Switch statement?
A switch statement considers a value and compares it against several possible matching patterns. It then executes an appropriate block of code, based on the first successful match. switch somevalue { case value 1: respond to value 1 case value 2, value 3: respond to value 2 or 3 default: otherwise do this }
Adds an element to an array.
Can also update a car in middle of array.
Var.append( )
Property of an array that counts the number of items in a given array.
Var.count
What is an array?
A sequential list of variables. Always starts at 0, increments by one in order.
Var todo[string] = [“call”,”suck”]
To add a value on end:
todo += [“swallow”]
A single operator that can affect the resulting value of a variable.
Unary operators
++ increment
– decrement
- negative
! Means variable or constant is negated
Example of a function with a return type String..
func heyName(name: String) -> String { var greeting = "Hello \(name)" return greeting }
Allows you to create a new type that groups several related values.
Enum.
enum Coin {
case Penny, Nickel, Dime, Quarter
}
Penny etc are called “Members”
What is a Switch statement?
A switch statement considers a value and compares it against several possible matching patterns. It then executes an appropriate block of code, based on the first successful match. switch somevalue { case value 1: respond to value 1 case value 2, value 3: respond to value 2 or 3 default: otherwise do this }
Adds an element to an array.
Can also update a car in middle of array.
Var.append( )
What is Concatenation? Provide an example.
Adding objects together:
var greeting = “Hello” + language
What is a mutable string? Provide an example.
A string that can be modified:
var language = “Swift”
What’s an immutable string? Provide an example:
A string that can’t be modified:
let language = “Swift”
What is a Struct? Provide an example.
A Struct allows you to create a new data type with stored properties:
struct Car {
var doors : Int
var wheels: Int
}
Create an instance of the struct Car
Hint–make a sedan.
var sedan = Car(doors:4, wheels:4)
When do you use an optional?
You use optional a in situations where a value may not be present. An optional says:
1) there IS a value, and it equals x, or
2) there ISNT a value at all (nil)
How do you make an optional?
Adding ? To a type makes it an optional.
How can we unwrap an optional?
The bang ! Operator unwraps an optional, but using if let to unwrap even better.
–note, needs example