Q1: What view would you use to display information that doesn't fit on the screen? Select all that apply
- UIView
- UILabel
- UIScrollView
- UITableView
Q2: Which view or control allows the user to enter a single line of text? Which one allows the user to enter multiple lines of text?
- UITextField
- UITextView
Q3: Which of the following allows you to execute code for a specific control event? Which allows you to access properties on a specific view or control?
- IBAction
- IBOutlet
- Intro to Protocols
- Protocols vs Inheritance
- Protocols in the Swift Standard Library
- Create and use Protocols
- Distinguish between Protocol Conformance and Inheritance
- Use protocols to describe properties and behaviors of a conforming type
- Identify common protocols in the Swift Standard Library
We start with class inheritance.
We realize we need more functionality in one class from two other classes.
We need multiple inheritance. Not supported 😟
Passing around classes can cause unexpected behavior. We want to avoid this as much.
Tight coupling.
Protocols are another type in Swift. 🙃
But they don't create any instances. Instead, they define a blueprint of methods, properties, and other requirements that other types need to implement.
A protocol can be adopted by a class, structure, or enumeration.
Any type that satisfies the requirements of a protocol is said to conform to that protocol.
Protocols are similar to contracts.
A contract is a guarantee that some terms will be satisfied. ✍🏼
Anything on the contract becomes a set of obligations to whoever "signs" it.
protocol FullName {
var fullName: String
func printToConsole(message: String) -> Bool
}
You can declare methods in a protocol. Notice you don't define an implementation. A protocol makes no assumption about the implementation details of any type that conforms to it. A protocol only defines the method name, it's parameters and return value.
struct Person: FullName {
var fullName: String
func printToConsole(message: String) -> Bool {
print(fullName)
return true
}
}
var author = Person(fullName: "Haruki Murakami")
struct Person: FullName{
var firstName: String
var lastName: String
var fullName: String {
return "\(firstName) \(lastName)"
}
func printToConsole(message: String) -> Bool {
print(fullName)
return true
}
}
var author = Person(firstName: "Haruki", lastName: "Murakami")
If a class inherits from another and also conforms to a protocol, all can be separated by commas and the inheritance comes first.
Create a protocol Perimeter
that defines a read-only property side
of type [Double]
.
Implement Perimeter
with structs representing Square
, and Circle
.
Add a circle and a square to an array, print their perimeters.
Notice, you have two different types but the array sees the type as the protocol so they are compatible!
- Create a model of a car, it should have:
- max speed
- number of wheels
- doors
- model
-
Generalize the car, create a model for a vehicle which will represent all vehicles, a truck, motorcycle & bus are vehicles
-
Create a model(struct) and instances of each a truck, motorcycle and bus.
Swift uses protocols extensively. We won't learn every single aspect of the Swift standard library, it makes no difference in our code's performance. But we can benefit from being familiar with several commonly used protocols.
In groups of X, get together to research about one of the following protocols. One per group.
Make sure everyone understands how they work and individually prepare an example in a playground. (it can be the same for everyone in the group).
Also make sure you can explain in words what it does, it might be helpful to have notes.
It will be thanks to your contribution that others can learn about what you researched.
-
Now split the group. Go and find people who did the other protocols.
-
We should have groups with people who know about all of them.
-
Take turns to have each person explain their protocol and show the example.
-
By the end of the activity you should be familiarized with all 4 protocols thanks to your peers. 😀
Any questions?
Given the Artist struct below, implement the Equatable protocol. You'll define what makes two instances equal.
// Used by Artist to determine style of Artist
enum Style: String {
case impressionism
case surrealism
case cubism
case popArt
}
struct Artist {
let name: String
let style: Style
let yearBorn: Int
}
// Example instances of Artists, use for testing your equatable
let monet = Artist(name: "Monet", style: .impressionism, yearBorn: 1840)
let dali = Artist(name: "Salvador Dali", style: .surrealism, yearBorn: 1904)
let andy = Artist(name: "Andy Warhol", style: .popArt, yearBorn: 1928)
Continue working in your final project.