Object-Oriented Programming in Swift Flashcards
What is another word for an object?
Class instances
What are objects?
Self-contained modules of functionality that can be easily used and re-used as the building blocks of a software application.
Instances consist of 1. _____ _____ called _____ and 2. _____ called ______. They can be accessed and called on the instance to perform tasks AND are collectively referred to as 3. _______ _______.
- Data variables, properties
- Functions, methods
- Class members
What does a class do? Specify
Defines what an instance will look like when it’s created. What methods will do and what properties will be.
What does the “properties” section of a class do?
Defines the variables and constants to be contained in the class.
What is the idea behind “data encapsulation”?
Data should be stored within classes and accessed only through methods defined in that class.
Data encapsulated in a class is referred to as _______ and _____
Properties, Instance variables
What’s the difference between a type method and an instance method?
- A type method operates at the level of a class, such as creating a new instance of a class.
- An instance method operates only on an instance of a class such as performing an arithmetic operation on two properties and returning the result.
Where and how is an instance method declared?
Within the opening and closing braces of the class to which it belongs using the standard Swift function declaration syntax.
How is a type method declared?
By preceding the declaration with the “class” keyword.
syntax to create an instance of a class
var instanceName: ClassName = ClassName( )
What does dot notation do?
Calls instance methods and accesses properties built into a class.
Syntax to access an instance variable or call an instance method?
classInstanceName.propertyName
Or
classInstanceName.instanceMethod( )
What are the two class property categories?
“Stored properties” and “computed properties”
What is a “computed property”?
A property that’s derived based on some form of calculation or logic at the point at which the property is set or retrieved.
How are “computed properties” implemented?
By creating “getter” and optional “setter” methods containing the code to perform the computation.
- What’s unique about a property being declared as “lazy”?
2. How is this advantageous?
- It will only be initialized when it’s first accessed.
2. It allows resource intensive activities to be deferred until the property is needed.