Create a class of objects

Every object of class MyDate will contain three stored properties inside of it: year, month, day.

Paste the following code into a playground, or into the application(_:didFinishLaunchingWithOptions:) method of the appliaction delegate.

class MyDate {
	var year: Int = 0;
	var month: Int = 0;
	var day: Int = 0;

	init() {   //An init method with no arguments.
	}

}

var d: MyDate = MyDate();   //Create a new object of class MyDate.  Call init.

print("\(d.month)/\(d.day)/\(d.year)");

d.year = 2018;
d.month = 10;
d.day = 20;

print("\(d.month)/\(d.day)/\(d.year)");
0/0/0
10/20/2018

An init that takes arguments

Insert the following init method after the no-argument init method.

	init(month: Int, day: Int, year: Int) {   //An init method with three arguments.
		self.year = year;    //self.year is the property, year is the argument
		self.month = month;
		self.day = day;
	}
Create the following object e after the existing object d.
var e: MyDate = MyDate(month: 10, day: 20, year: 2018); //Call the 3-arg init method.

print("\(e.month)/\(e.day)/\(e.year)");
10/20/2018

An instance method that returns nothing

Note that a method of an object is allowed to mention the properties (year, month, day) of the object.

	//Change the contents of this object to the next date.
	//Assume that every month has 30 days.

	func next() {
		if day < 30 {
			day += 1;  //means day = day + 1;
		} else {
			day = 1;   //Go to the first day of the next month.
			if month < 12 {
				month += 1;	
			} else {
				month = 1;  //Go to the first month of the next year.
				year += 1;
			}
		}
	}
var e: MyDate = MyDate(month: 10, day: 20, year: 2018);
print("\(e.month)/\(e.day)/\(e.year)");
e.next();
print("\(e.month)/\(e.day)/\(e.year)");
10/20/2018
10/21/2018

An instance method that contains an array

	//Change the contents of this object to the next date.
	//Assume that this year is not a leap year.

	func next() {
		let length: [Int] = [
			 0,   //Unused.  Lets Jan have subscript 1, Feb to have 2, etc.
			31,   //January
			28,   //February,
			31,   //March
			30,   //April
			31,   //May
			30,   //June
			31,   //July
			31,   //August
			30,   //September
			31,   //October
			30,   //November
			31    //December
		];

		if day < length[month] {
			day += 1;
		} else {
			day = 1;   //Go to the first day of the next month.
			if month < 11 {
				month += 1;	
			} else {
				month = 1;  //Go to the first month of the next year.
				year += 1;
			}
		}
	}
var f: MyDate = MyDate(month: 2, day: 28, year: 2018);
print("\(f.month)/\(f.day)/\(f.year)");
f.next();
print("\(f.month)/\(f.day)/\(f.year)");
2/28/2018
2/29/2018

Let the array be a stored property.

Add another method that uses the array.

	func monthsInYear() -> Int {   //a method that returns an Int
		return length.count - 1;   //minus the dummy element
	}
print("d.monthInYear = \(d.monthsInYear())");
d.monthInYear = 12

To permit the array to be mentioned in the bodies of more than one method, let the array be a stored property of the class. Move the array to imediately before var year: Int = 0;.

Let the array be a type property.

There’s no need to burden every object (d, e, etc.) with its own copy of the array. Have them share a single copy.

	static let length: [Int] = [

Then in the next and monthsInYear methods, change length to MyClass.length.

Let monthsInYear be a type method.

monthsInYear can be a type method because it does not mention the instance properties year, month, day. A type method also cannot mention the keyword self.

	class func monthsInYear() -> Int {
		return length.count - 1;   //minus the dummy element
	}
Change
print("d.monthInYear = \(d.monthsInYear())");
to
print("MyDate.monthInYear = \(MyDate.monthsInYear())");
MyDate.monthInYear = 12

prev

Download MyDate.zip and open it in Xcode. Give class MyDate an instance method named prev. It will be just like next, except it will go backwards one day.