type
Post
Created date
Jun 15, 2022 06:01 AM
category
IT
tags
Programming
status
Published
Language
English
From
Original
summary
slug
password
Author
Jason Ching Yuen Siu
Priority
! Urgent + ! Important
Featured
Featured
Cover
Origin
Type
URL
Youtube
Youtube
icon
 
My first app is a simple "Hello World" style application that asks the user for one’s birthday and name, and then displays a message based on this.
Here is the instruction I followed, which is from my class.
 

By following this, you should be able to make a simple app. Following, I will explain how I did the extension tasks.

This consists of 3 parts.
 

Part 1 — Replace age with birth date in the Person class

1. In the person class, replace the “age” property with a property called “birthDate” of type “Date”. Modify the initialiser to take the birth date as an argument.
1. In the person class, replace the “age” property with a property called “birthDate” of type “Date”. Modify the initialiser to take the birth date as an argument.
2. Modify the “greeting” method to display the birthday. 3. Ensure the display of the birth date in the greeting nicely formatted.
2. Modify the “greeting” method to display the birthday. 3. Ensure the display of the birth date in the greeting nicely formatted.
 
4. Modify the label and outlet name for the ageField to make it be a field for entering birth date. 5. Modify the ViewController sayHello method to convert the text from the birthdate field into a Date instance. This can be again done with the DateFormatter class.
4. Modify the label and outlet name for the ageField to make it be a field for entering birth date. 5. Modify the ViewController sayHello method to convert the text from the birthdate field into a Date instance. This can be again done with the DateFormatter class.
  • Hint: You will need to specify the “dateFormat” property to tell it the format of date strings you are converting from. The “Date Formatting Guide” documentation (linked from the DateFormatter docs) can be difficult to understand, but the dateFormat you need to specify is “yyyy-MM-dd”. We suggest playing around with this class in a Swift Playground to see how it works for decoding different date and time strings.
6. Make sure there is error checking for entry of non-date strings in the field.
7. Build and test the app.
8. Once the app works, commit your work.

Part 2 — Calculate and show person’s age

1. Read the documentation of the Date class and figure out how to get a Date instance representing the current date.
2. Create a new method in the Person class that calculates and returns their current age in years (an Int).
  • The Calendar class has a useful method called “dateComponents” that returns the different between two dates, for each specified component, e.g., days, months, etc.
  • The “dateComponents” method needs to be called on an instance of Calendar. You can the standard instance via the static “Calendar.current” class property.
  • The components that you want it to return must be specified as a Set of enum values. A Set literal looks just like an Array literal, i.e., a one or more values separated by commas, enclosed in brackets.
notion image
notion image
3. Expand the Person class greeting to use the new method to display the person’s age.
4. Write another convenience method to calculate the number of days the person has been alive. Then expand the greeting in the Person class to report this.

 

Part 3 — Use a Date Picker for date entry

  1. Add a Date Picker object to your View Controller storyboard. Don’t bother replacing/deleting the birth date text field.
  1. Add constraints to position the date picker to the bottom of the interface.
notion image
3. In the Attributes Inspector, set the Date picker mode to Date, and Preferred Style to Wheels.
3. In the Attributes Inspector, set the Date picker mode to Date, and Preferred Style to Wheels.
4. Add an outlet so you can interact with the date picker in your view controller code.
4. Add an outlet so you can interact with the date picker in your view controller code.
5. In your View Controller sayHello method, comment out the code that gets the birthdate from the text field and instead get the date from the date picker. See the documentation for the UIDatePicker class if necessary.
5. In your View Controller sayHello method, comment out the code that gets the birthdate from the text field and instead get the date from the date picker. See the documentation for the UIDatePicker class if necessary.
 
 
PIXAR - Storytelling frameworkIOS colour chooser