Create a class and an object in Swift

The file Date.swift in this app creates a Swift class named Date. The class demonstrates the most common features of a class: stored properties, computed properties, and property observers; instance methods and type methods.

We are inventing this new class Date only to demonstrate how to create a class. Our class Date is not as good as Apple’s class Date which belongs to Apple’s Foundation framework. For the rest of the course, we will be using Apple’s class Date.

In the method application(_:didFinishLaunchingWithOptions:) of the class AppDelegate in the file AppDelegate.swift, we demonstrate what our new class Date can do. We create several objects of this class, read and write their properties, and call their methods. We also call a type method of our new class Date.

This app does not write to the simulator’s screen. We’ll do that in the next app.

Source code in

  1. AppDelegate.swift: added code to the method application(_:didFinishLaunchingWithOptions:) to demonstrate class Date.
  2. ViewController.swift: unchanged.
  3. Date.swift: this file creates class Date, which is a subclass of class NSObject. An init method of the subclass always calls an init method of the superclass—after the properties of the subclass have been initialized.

Create the project in Xcode 10.1 on OS X 10.14

Launch Xcode.

File → New → Project…

Choose a template for your new project:
Single View App

Choose options for your new project:
Product Name: Date
Team: Mark Meretzky
Organization Name: NYU SPS (this goes in the copyright notices)
Organization Identifier: edu.nyu.sps
Bundle Identifier: edu.nyu.sps.Date
Language: Swift
Devices: iPhone
☐ Use Core Data
☐ Include Unit Tests
☐ Include UI Tests

Save the project on your Mac’s Desktop.
Source Control: ☑ Create Git repository on my Mac

There should now be a new folder named Date on your Desktop. Since we created a Git repository, the folder should contain a subfolder named .git. To verify this, open the Mac Terminal application and say

cd ~/Desktop
ls -ld Date

cd Date
ls -l
ls -la

In the upper left corner of Xcode, select a simulator or device:
Date > iPhone XR

Create a new Swift class

Create a new Swift file named Date.swift with an uppercase D, and a new Swift class in it named Date. The left panel of Xcode is called the Project Navigator. If you don’t see the Project Navigator, select
View → Navigators → Show Project Navigator
In the Project Navigator, click once on the yellow Date folder to highlight it.
File → New → File…

Choose a template for your new file:
Swift File
Save As: Date.swift

The yellow Date folder in the Project Navigator should now contain a new file named Date.swift. Drag Date.swift to a position immediately below ViewController.swift, just to keep all the .swift files next to each other.

In the Project Navigator, select the new file Date.swift. Edit this file in the center window of Xcode. Also edit AppDelegate.swift.

Swift naming conventions

The name of a Swift variable starts with a lowercase letter. The name of a Swift class starts with at least one uppercase letter. The name of a Swift class that we create starts with one uppercase letter. The name of a Swift class created by Apple starts with a pair of uppercase letters. The most common examples are:

  1. CG: Core Graphics
  2. NS: Next Step. Apple changed the name to Foundation.
  3. UI: User Interface
  4. CL: Core Location, which we mentioned briefly here.

Things to try

  1. Make sure the Scheme Menu in the upper left corner of Xcode says
    Date > iPhone XR
    Then run the above project by pressing the triangular Run button in the upper left corner of Xcode. When the simulator appears, you can resize it by dragging on its lowre right corner, or by pulling down
    Window → Physical Size
    The output of the print statements should appear in the Debug Area at the bottom center of Xcode. If you do not see the Debug Area, bring Xcode to the front and select
    View → Debug Area → Show Debug Area
    A year has 12 months.
    Today is 10/17/2018.
    Today is 10/17/2018.
    Today is day number 17 out of 31 in month number 10.
    The second day of this month is 10/2/2018.
    Independence Day was 7/4/1776.
    America was one month old on 8/4/1776.

  2. In the willSet property observer for the month property of class Date, change the 12 to Date.yearLength().

  3. Add the following two methods to class Date, and write some code to demonstrate them.
    1. a method named prev, with no parameters. It should be exactly like the existing method named next with no parameters, except that it moves the object 1 day back in time instead of 1 day forward.
    2. a method named prev, with one Int parameter. It should be exactly like the existing method named next with one Int parameter, except that it moves the object n days back in time instead of n days forward.

  4. Insert the following statements into the viewDidLoad method of the ViewController in the ViewController.swift file immediately after the “Do any additional setup” comment. Then run the project again. Experiment with yellow and green. Make the view semi-transparent so you can see the black window behind it.
    		//view is a stored property of the ViewController object.
    		view.backgroundColor =;
    		//0.0 is totally transparent, 1.0 is totally opaque
    		view.alpha = 0.85;	//or try 0.35

  5. If you have figured out how the monthLength method of class Date works, change the yearLength method to the following.
    	class func yearLength() -> Int {
    		let calendar: Calendar = Calendar.current;
    		let dateComponents: DateComponents = DateComponents();
    		let date: Foundation.Date = dateComponents)!;
    		let range: Range = calendar.range(of: .month, in: .year, for: date)!;
    		return range.count;