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
- 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.
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
- Add a Date Picker object to your View Controller storyboard. Don’t bother replacing/deleting the birth date text field.
- Add constraints to position the date picker to the bottom of the interface.
- Author:Jason Siu
- URL:https://jason-siu.com/article%2F92559d00-9a4e-4f58-87fb-e420208c4e8f
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!
Relate Posts