Coder Social home page Coder Social logo

swift-extensions-lab-dumbo-web-111819's Introduction

#Extensions Lab

Overview

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

  1. Create an extension function for the String class called whisper()
  • Returns a String in lowerscase to create a whispering effect
  1. Create a String extension function for the String class called shout()
  • Returns a String in upperscase to create a shouting effect
  1. Create a String extension computed property for the String class called pigLatin
  • 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
  1. Create an Int extension computed property for the String class called points
  • 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
  1. Create an extension function for the Int class called isDivisible(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
  1. Create an extension computed property for the Int class called squared
  • Squares itself
  1. Create an extension computed property for the Int class called halved
  • Halves itself by calling a function

##Using Your Extensions (this is not tested) In ViewController.swift inside the function body of viewDidLoad():

  1. Create a String object with the value as your full name called fullName
  2. Create an Int object with the value 8675309 called phoneNumber
  3. Print the value of the extension computed properties on fullName and phoneNumber
  4. Call all of your String and Int 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 called unicornLevelLabel. This represents the object we're going to display our text in.
  • unicornLevelLabel has a property text set the value of the property to your name object's unicornLevel 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.

swift-extensions-lab-dumbo-web-111819's People

Contributors

annjohn avatar daveneff avatar jimcampagno avatar johann avatar johannkerr avatar susanlovaglio avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.