Unit 1 Swift. Playground Class Presentataion
Unit 1 Swift. Playground Class Presentataion
Unit 1
Introduction to Programming
Building Blocks
Variables
Integer
Float
Boolean
Class
Object
Basic Building Blocks
Variables
Bank account balance, it shows the current amount of money you have
available. Your bank account may be $100 at the beginning of the month,
and $350 on your payday.
This is an example of a variable.
A variable is a representation of a value.
Variables come in many different shapes and sizes.
Different types of variables hold different types of values.
Numbers, letters, words, true, false, or even a custom car.
Integer
An integer is a whole number, a number without any decimal places,
positive or negative: Example -10, 0, 100, 21031
An integer could be used for the number of stars for a movie review, a
house’s street address, or the score for a sports team.
Basic Building Blocks
Float
need to be more precise with a value.
In the case of currency, a decimal place is used to keep track of the cents on a
dollar $10.51
A decimal-based variable is called a float. Float is short for floating point,
another name for a decimal place
Boolean
Respond with either Yes or No.
cannot respond with 7, $103.45, or “Banana.”
Yes or No variable is referred to as a Boolean.
Similar to a light switch—they are either on or off,true or false; there is no in-
between value
Basic Building Blocks
String
Name - collection of letters that form a set of words
Eg:"SteveDerico"
A string is used to represent characters strung together to make words
and sentences.
A string can hold a series of letters, numbers, and symbols. Strings are
surrounded by a pair of quotes. For example:
"Steve is cool."
"Where is the ball?"
"Go Giants
Basic Building Blocks
Classes
Different types of automobiles –different –few core
characteristics
wheels, an engine, and brakes
Core characteristics are called attributes.
purpose and provides value to the consumer.
A car can drive, honk, brake, and steer. Basic methods are
available in every car.
Without these methods, it would not be a car. These core methods
are called behaviors.
Designing a new car, a blueprint would be a good place to start.
Blueprint is a document that serves as a template for building
something. Blueprint defines the attributes and behaviors of the car
Basic Building Blocks
Car blueprint
Methods
computer processes a list of steps to achieve a task. This
list of steps is called a method.
A method is a collection of code to complete a specific
task
Example –Cooking Process
Basic Building Blocks
Car
Inheritance Wheels Standard
Engine Standard
passing down of attributes and behaviors from
Brakes Standard
parent to child is called inheritance. Can Drive Forward
Inheritance is the ability for a class to extend or Can Stop
override functionality from a parent class. Can Steer Left and Right
Example - class you created was the parent of SUV
the SUV class. Wheels Mud
SUV class will inherit all the attributes and Engine V8
behaviors of the car class. SUV will also be Brakes Standard
Drive System All-Wheel
able to add its own attributes and behaviors.
Can Drive Forward
Can Stop
Can Steer Left and Righ
Can Drive Uphill
Can Tow a Boat
Basic Building Blocks
varnumberOfYears: Int
Set a value to the variable. The value you set must match the type
of variable; otherwise, the variable will not be created:
varnumberOfYears: Int = 30
An equals sign is used to initialize a variable.
Initialize means to set up or provide the default value to a variable.
Integers
An integer, a whole number positive or negative, uses the Int keyword.
var keyword to create a variable with favoriteNumber as the variable
name. Then the colon is used to state the type.
The variable’s value is set to 4 using the equals sign:
varfavoriteNumber: Int = 4
Float
A float, a number with a decimal place, uses the Float keyword.
var keyword to create a variable and sets accountBalance as the
variable name. colon is used to set the type using the Float
keyword.
Variable’s value is set to 1203.51 by using the equals sign:
varaccountBalance: Float = 1203.51
Boolean
A Boolean, a variable with the value true or false, uses the Bool
keyword.
var keyword to create a variable and sets isStudent as the variable
name.
colon is used to state the type using the Bool keyword. Finally, the
variable’s value is set to true by using the equals sign:
varisStudent: Bool = true
Strings
varnumberOfYears = 30
let name = "Steve"
let isMale = true
varbankAccountBalance = 1034.20
Modifying Strings
String class provides convenient methods like uppercaseString,
where the string is changed to its uppercase value.
// show the variable after it has been updated
Execute sequentially
Appending Strings
string variable and add on to the end of it. This is called appending
create new, longer strings, add string variables between other strings
Variables in Strings
If else
if isWeekday == true {
getReadyForWork()
if isMale == true { } else if isSaturday == true {
bow() goRunning()
}else{ } else {
curtsy() goToYoga()
} }
Optionals
Variables that could result in the absence of a value
Ex :Translation output –no
valueless state is called nil
used with any type of variable and are explicitly declared using the ?
symbol next to the type keyword.
vartranslatedWord: String?
Process of unwrapping an optional helps to remind the developer to
check and make sure the variable is not nil. There are two steps
Ex:Race Car
Methods
Methods are a group of steps that work together to accomplish a
specific task.
Methods are very similar to functions. Methods are a set of steps to
complete a task inside a class or object.
A function is a set of steps to complete a task that stands alone. These
two items are so similar, they have become synonyms.
“method” and “function” interchangeably
Methods
funcgoodMorning() {
println("Good Morning")
}
Parameters are defined by first stating the variable name and then a colon followed by
the type of variable.
}
Default value
Default value to parameter, add an equals sign next to the type and
provide the default value.
goodMorning()
Return Values
Methods can do more than accept input and execute steps
Generate output When a method provides an output value, it is called a
return value.
Return value is the final product of the method, and it is sent back to
the caller.
A return value can be any type, but the return type must be declared by
the method
funcsum(a: Int,b: Int) ->Int{
return a + b
}
class House {
varexteriorColor= "Brown"
varcurrentAirTemp= 70
varisAlarmEnabled= false
funcprepareForNighttime() {
isGarageOpen = false
isAlarmEnabled = true
currentAirTemp = 65 }
Creating an Object
Create an object from a class, call the initializer method.
The initializer method is a method designed specifically for setting
up new objects.
If user provided default values for the properties, there is no need to
write an initializer method.
Xcode will create for you.Theinitializer method is typically
accessed by writing the name of the class followed by two
parentheses
varmyHouse: House = House() or varmyHouse = House()
Accessing Properties
Access the value of a property by using dot syntax. Dot syntax is
a format used to access and assign values to properties.
Dot syntax begins with an object
varmyHouse = House()
myHouse.exteriorColor
myHouse.exteriorColor = "Blue"
Calling Methods
myHouse.prepareForDaytime()
Subclasses
Reuse a template instead of starting from scratch
Start with the parent class and then make changes from there.
This process of reusing is called subclassing
Subclassing provides a convenient way to share attributes and
behaviors between a parent and child class
class Cabin: House {
}
Inheritance
Inheritance is the ability to pass down attributes and behaviors
from a parent class to a child class.
All of the parent class’s properties and methods will
automatically be shared with the child class.
Sub class can add or change these if needed
Overriding
Changing the inherited properties or methods in a child class is called
overriding.
Overriding allows the child class to define its own version of a
particular property or method.
To override a method, add the override keyword.
The cabin class defines its own initializer method by using the
init keyword.
cabin class overrides the initializer method from the House
class, the override keyword is added to the beginning of the
method name.
initializer method is automatically created for the House class.
The init keyword is followed by a set of empty parentheses, and
an open brace will begin the initialize method.
Parent class’s initializer should first be called before the child class
overrides or makes changes.
Refer to the parent class, use the super keyword. Inside the child
class’s init method, write super.init().
This will call the parent class’s initializer method.
Once this is complete, the child class is free to override and make
changes. In this case, the cabin class sets the exteriorColor to
"Red".
Overriding methods
override a subclass’s methods.