#Extensions
##Objectives
- Practice creating extensions on classes
- Practice calling and printing values associated with extensions created
##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 sentances
- 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
###Int
2. Create an extension function for the Int
class called half()
- Halves itself
- Create an extension function for the
Int
class calledisDivisibleBy()
- 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
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!