"Extra argument in call" error when passing argument to method

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]; 

he will write the start of this week on startOfTheWeek and the duration of the weeks before interval .

I am writing on the playground

 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 that signature

 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 it gives an error

"could not find an overload for 'rangeOfUnit' that takes the provided arguments.

+6
source share
1 answer

Try the following:

 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 does not have an external name and therefore should not be called.)

+8
source

Source: https://habr.com/ru/post/970456/


All Articles