Reputation: 1015
I'll keep this as short and simple as I can.
I have an NSDate extension that I use for a number of things throughout my app, but I have run into a problem with (strangely) a very simple function.
The relevant part of the extension is:
import Foundation
extension NSDate {
func nineAMMonday() -> NSDate {
let greg = NSCalendar.currentCalendar()
let componenets = NSDateComponents()
componenets.weekday = 2
componenets.hour = 9
componenets.minute = 0
componenets.second = 0
return greg.dateFromComponents(componenets)!
}
}
Its purpose is simply to return an NSDate which is referring to a Monday morning.
So far so good...
The issue is when I try to call this function, I get an error:
Missing Argument For Parameter #1 In Call
I read here https://stackoverflow.com/a/26156765/3100991 about someone who had a similar problem, however their code was long and confusing and I didn't understand the answer. I think it would be useful for the general community if a simple explanation and fix was provided.
Thanks for your help in advance!
Loic
Upvotes: 2
Views: 2567
Reputation: 2482
I just try that (in the playground) and it works. I think your error is in the call of your method. since your method is an instance method you have to call it on an existing date object. So try to call it like that:
import Foundation
extension NSDate {
func nineAMMonday() -> NSDate {
let greg = NSCalendar.currentCalendar()
let componenets = NSDateComponents()
componenets.weekday = 2
componenets.hour = 9
componenets.minute = 0
componenets.second = 0
return greg.dateFromComponents(componenets)!
}
}
// Call
var date = NSDate();
date.nineAMMonday()
But in your case the best thing to do is a class func.
import Foundation
extension NSDate {
class func nineAMMonday() -> NSDate {
let greg = NSCalendar.currentCalendar()
let componenets = NSDateComponents()
componenets.weekday = 2
componenets.hour = 9
componenets.minute = 0
componenets.second = 0
return greg.dateFromComponents(componenets)!
}
}
// Call
NSDate.nineAMMonday()
Upvotes: 2