- Create a new class with readonly properties (private setters), an optional property, and a calculated property.
- Write a designated initializer, convenience initializer, and default initializer that are correctly related.
- Write internally-visible methods that provide controller interaction with the readonly properties.
- Use a set as a property when handling a collection of unique values.
Open the swift-people.xcworkspace
file.
Generate a new Swift file for the Person
class.
Declare the Person
class with four readonly properties:
name
, an immutableString
,ageInYears
, a mutableInt
optional,skills
, an empty mutableArray
ofString
s,qualifiedTutor
, a calculated property of typeBool
that, for now, simply returnsfalse
.
-
Write a designated initializer that takes values for the
name
andageInYears
properties. Top-tip: Remember thatageInYears
is an optional property. -
Write a convenience initializer that takes an argument for
name
, but notageInYears
. This should call the designated initializer with anil
value forageInYears
, since no information was collected for it. -
Write a default initializer that calls the designated initializer to set the
name
property to"John Doe"
andageInYears
tonil
.
-
Review the provided method in the
Int+Ordinal.swift
file. This contains an extension of theInt
class that returns a two-letter string associated to the integer's pronunciation (called an "ordinal"). You will need to use this method to solve this challenge. -
Write a method named
celebrateBirthday()
that returns aString
. If theageInYears
property containsnil
, it should return a happy-birthday message with the person's name formatted like this for Jenny:
HAPPY BIRTHDAY, JENNY!!!
Hint: Make sure to upcase the person's name.
- However, if
ageInYears
contains an integer value (notnil
), this method should add one to theageInYears
property, determine the ordinal for the property's new value, and then return a string that, for Mark's 30th birthday, would read:
HAPPY 30TH BIRTHDAY, MARK!!!
Hint: You will need to upcase both the ordinal and name.
Now, write a series of five methods that add a specific string (each representing a different programming skill) to the person's skills
property. You will need to protect against duplicating the entries and there are two ways to do this:
- Use an
if
statement to check if the skill is already in theskills
array (i.e. use the.contains()
method which returns aBool
). - (Advanced) Convert the
skills
array to a set of typeString
instead. Sets implicitly require uniqueness of their collected values, so a duplicate addition will actually cause no change.
- Hint: To initialize an empty set, use the syntax:
Set<Type>()
. - Hint: To add an element to a set, use the
.insert(Element)
method.
The five methods you should write, with no arguments and no returns, along with their associated strings to add to the person's skills are:
Method Name | Skill String |
---|---|
learnSkillBash() |
"bash" |
learnSkillXcode() |
"Xcode" |
learnSkillObjectiveC() |
"Objective-C" |
learnSkillSwift() |
"Swift" |
learnSkillInterfaceBuilder() |
"Interface Builder" |
We want to allow our experienced Person
s to help out those who are just starting out. Make the qualifiedTutor
calculated property return true
if the person knows at least four (4) skills.
Top-tip: Avoid using an if
statement for this, remember that comparison operators return booleans.
View Swift People on learn.co and start learning to code for free.