#Extensions Lab
In this lab, you'll practice creating extensions on classes, and calling and printing values associated with those extensions.
##Instructions
Open the workspace and create a new empty Swift file called Extensions
. This is where you're going to create all of the extensions. Remember to run often but without the declaration of the objects and functions listed below you won't be able to run the tests. But you can always use the console to make sure your getting the correct output.
###String
- Create an extension function for the
String
class calledwhisper()
- Returns a
String
in lowerscase to create a whispering effect
- Create a
String
extension function for theString
class calledshout()
- Returns a
String
in upperscase to create a shouting effect
- Create a
String
extension computed property for theString
class calledpigLatin
- Takes the first letter of a word and moves it to the end
- Adds the letters "ay" to the end of the word
- Should be able to handle sentences
- If it's a single letter, it should return the character
- Create an
Int
extension computed property for theString
class calledpoints
- Should calculate the amount of points a given string has
- 1 point for consonants and 2 points for vowels
- Ignores spaces and numbers, returns zero for empty strings
- Hint - This is still an extension on the String class
- Note - Y is to be taken as a consonant even though it frequently carries a vowel sound. See Jim for more info
###Int
2. Create an extension function for the Int
class called half()
- Halves itself
- Create an extension function for the
Int
class calledisDivisible(by:)
with the name of the argument being number.
- To find out if the recipient
Int
is divisible by the argument - Takes in an
Int
argument - Returns a
bool
- Create an extension computed property for the
Int
class calledsquared
- Squares itself
- Create an extension computed property for the
Int
class calledhalved
- Halves itself by calling a function
##Using Your Extensions (this is not tested)
In ViewController.swift
inside the function body of viewDidLoad()
:
- Create a
String
object with the value as your full name calledfullName
- Create an
Int
object with the value 8675309 calledphoneNumber
- Print the value of the extension computed properties on
fullName
andphoneNumber
- Call all of your
String
andInt
functions on your name and print them to the console
###Emoji
1.Create a computed extension property on String
called unicornLevel
- Calculates one unicorn for each character in the recipient
String
- Use this totes adorbs unicorn emoji ๐ฆ directly in your code. For example:
let unicornPhrase = "My ๐ฆ is awesome"
print(unicornPhrase)
Console output: My ๐ฆ is awesome
2.You probably haven't interacted with UIView
much or maybe not at all, but let's get our unicorn status to be displayed in the simulator. Don't worry too much about how it's doing it just yet. You'll get into that later on!
In the ViewController.swift
:
- You'll see that we've added something called an
IBOutlet
to the top calledunicornLevelLabel
. This represents the object we're going to display our text in. unicornLevelLabel
has a propertytext
set the value of the property to your name object'sunicornLevel
property- Run the program and see your unicorn level displayed in the simulator!
View Extensions Lab on Learn.co and start learning to code for free.