Swift Components Tour – NSCalendar

In this lesson

Back in the Swift Components Tour app, we are talking about how to work with dates. Your choices are NSDates, NSDateFormatters, and NSCalendars (or data, presentation, and interaction). Today, it’s NSCalendars (or interaction)!

Kyle Roberts
Swift Guru at Large

Kyle's Series

Transcript

Tap on time to skip ahead

00:10

Hello world! Kyle here with Brax.tv and today we are going to talk about the NSCalendar object and also include a little bit with NSDateComponents. We are on the 3rd most important class, or method, or way of working with NSDates, and we’ve covered NSDates and NSDatesFormatters. Now we are going to talk about NSCalendars. And the way we can divide these up is NSDate is a way to store a date. NSDateFormatter is a way to present a date. And using NSCalendar and NSDateComponents is a way to work with or edit dates.

00:57

What I’m doing on viewDidLoad of the UISteppersViewController is creating a new NSCalendar using the Gregorian calendar identifier so that this NSCalendar is using the Gregorian calendar. I’m creating an NSDateComponent object using the current date and this components object, I’m only going to worry about the day. In this case it is October 2, 2015. This components object will have a property called day that will be set to the value 2. We are setting the date steppers value property to a double of the components.day property.

01:40

Behind the scenes in this view controller the value that this stepper is keeping track of is going to be the day of the month.

01:52

And we can see that that’s important down here when in the adjustDate method, which is called any time we press the plus or minus button of this date stepper here. In this method what’s going on is that I’m getting another NSDateComponents object on the component day of the date shown in the date label here. Let’s actually change this a few days. Thursday, October 8, 2015. So that this date components object here will have a property called day with the value of 8. Because that is the day of the NSDate displayed in this label.

02:37

What we are actually going to do is compare that components.day to the date steppers value. As we saw in viewDidLoad, we were setting the value to the NSDate components day property of whichever day that the date was on. I’m actually going to put a breakpoint here. And I’m going to press this one more time. So, we’ve pressed this so that at the end of this execution the date will increment to October 9. But, right now the date steppers value property is going to be eight. So after we’ve hit this plus button, UIKit is automatically going to increment this stepper by the value that we told it too. And it’s going to increment it by one.

03:28

With this logic down here, we’re going to check the components. The NSDateComponent date of the date in the date label against the value property of the NSDateStepper. Since we hit the plus button, the components.day value is actually going to be less than the date steppers current value. And in this situation, the components.day value is eight and the UIStepper’s value is going to be nine.

03:56

We will end up in the “else” block of this “if” statement. Basically the difference between the “if” block and the “else” block here in the “if” statement is, in the “else” block we are going to be adding one day and above that in the “if” part of the “if” statement we are going to be subtracting one day.

04:16

Since we hit the plus button we are going to be adding a day. And the way we do that is again using NSCalendar and we’re assigning this currently nil steppedDate object to an NSDate that is one day in the future of the date that we pass in. And we are doing that using this date by adding unit method and we are specifying the unit that we want to add, which is day. We are specifying the value that we want to add, which is just one, and then we are passing in the NSDate that we want to make that addition too. So that is the NSDate that we got from the date label here. It’s pointing to October 8th. And since we are incrementing the day by positive one, when we continue this program execution, it is going to say October 9. So the NSDate of this date label is actually going to point to October 9.

05:13

Almost the same thing happens when we go back by one day. We are using this date by adding unit method. Specifying the unit, day. The value that we pass in is now negative one, since we want to go back one day. We are editing this date that we got from the date label. And we are passing in an option called search backwards. This only comes into play when you are going to go backwards over a month. If you press the minus button here it should go to September, which it does. But if we hadn’t specified that search backwards method, it’s only going to search forwards. So at the end of the month or at the beginning of the month if you were going to go backwards to a previous month, the NSCalendar would actually see that “Hey, this number, since we’re only searching forward, the next time we come across this number is the next month.” Instead of saying September 30, it would actually say November 30. But, since we’ve specified this search backwards option, it’s going to look backwards. It correctly went to September.

06:26

I’m just getting an NSDateComponents object again on the date component using this new step date object that we got from this “if” statement. And, then I am setting the correct values on the stepper and the date label here. Even though this was a specific example I hope it showed how you can use an NSCalendar and NSDateComponents to edit and work with a date. And it’s easy enough to use other elements of a date. We can actually specify the year, the month. Can you specify the time? I think you can specify the time. You can use any value you want. You don’t have to go one day forward or one day back. One hour forward or one hour back. You can do what you need to do with this. If you wanted to advance by year, you could specify in this date by adding unit method. You can set the unit to year and then the value to one. Or, you could even set the unit to month and then add a value of 12, so that it would add 12 months to the current date which is the same as adding one year.

07:39

There’s a lot that you can do with it but it’s going to follow a lot of the same structure, at least when editing a date like this. So hopefully you now know after these three videos enough to work with dates on your own. Or, at least get started with working with dates on your own. And I’ll see you next time. Thanks for watching.

Additional Info

Register to get access to additional resources and info.