I am trying to write the following Objective-C code in swift:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDate *startOfTheWeek;
NSTimeInterval interval;
[cal rangeOfUnit:NSWeekCalendarUnit
startDate:&startOfTheWeek
interval:&interval
forDate:now];
it will write the beginning of this week to startOfTheWeek
and the weeks duration to interval
.
In the playground I write
let now:NSDate = NSDate()
var startDate:NSDate
var duration: NSTimeInterval // also tried "var duration: CMutablePointer<NSTimeInterval>"
let cal = NSCalendar.currentCalendar()
cal.rangeOfUnit(unit: NSCalendarUnit.WeekCalendarUnit, startDate: startDate, interval: duration, forDate: now)"
Although code completion tells me the signature is
cal.rangeOfUnit(<#unit: NSCalendarUnit#>, startDate: AutoreleasingUnsafePointer<NSDate?>, interval: <#CMutablePointer<NSTimeInterval>#>, forDate: <#NSDate?#>)
an error occurs saying Extra argument 'interval' in call
What am I doing wrong?
I also tried
cal.rangeOfUnit(NSCalendarUnit.WeekCalendarUnit, startDate: startDate, interval: duration, forDate: now)
but this yields the error
"could not find an overload for 'rangeOfUnit' that accepts the supplied arguments."
Try this:
let now = NSDate()
var startDate: NSDate? = nil
var duration: NSTimeInterval = 0
let cal = NSCalendar.currentCalendar()
cal.rangeOfUnit(NSCalendarUnit.WeekCalendarUnit, startDate: &startDate,
interval: &duration, forDate: now)
(By default, the first parameter has no external name and thus should not be named.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With