Coder Social home page Coder Social logo

swift-people-dumbo-web-062518's Introduction

Swift โ€” People

Objectives

  1. Create a new class with readonly properties (private setters), an optional property, and a calculated property.
  2. Write a designated initializer, convenience initializer, and default initializer that are correctly related.
  3. Write internally-visible methods that provide controller interaction with the readonly properties.
Advanced
  1. Use a set as a property when handling a collection of unique values.

Instructions

Open the swift-people.xcworkspace file.

I. Create the File, Class, and Properties

Generate a new Swift file for the Person class.

Declare the Person class with four readonly properties:

  • name, an immutable String,
  • ageInYears, a mutable Int optional,
  • skills, an empty mutable Array of Strings,
  • qualifiedTutor, a calculated property of type Bool that, for now, simply returns false.

II. Write the Initializers

  1. Write a designated initializer that takes values for the name and ageInYears properties. Top-tip: Remember that ageInYears is an optional property.

  2. Write a convenience initializer that takes an argument for name, but not ageInYears. This should call the designated initializer with a nil value for ageInYears, since no information was collected for it.

  3. Write a default initializer that calls the designated initializer to set the name property to "John Doe" and ageInYears to nil.

III. celebrateBirthday()

  1. Review the provided method in the Int+Ordinal.swift file. This contains an extension of the Int 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.

  2. Write a method named celebrateBirthday() that returns a String. If the ageInYears property contains nil, 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.
  1. However, if ageInYears contains an integer value (not nil), this method should add one to the ageInYears 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.

IV. learnSkill... Methods

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:

  1. Use an if statement to check if the skill is already in the skills array (i.e. use the .contains() method which returns a Bool).
  2. (Advanced) Convert the skills array to a set of type String 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"

V. Redefine the qualifiedTutor Property

We want to allow our experienced Persons 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.

swift-people-dumbo-web-062518's People

Contributors

markedwardmurray avatar joelconnects avatar misterfifths avatar ipc103 avatar

Watchers

Kevin McAlear avatar  avatar Victoria Thevenot avatar Belinda Black avatar  avatar Joe Cardarelli avatar Sam Birk avatar Sara Tibbetts avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Jaichitra (JC) Balakrishnan avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Nicole Kroese  avatar  avatar Lore Dirick avatar Nicolas Marcora avatar Lisa Jiang 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.