Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "extra argument in call" when passing argument to method

Tags:

swift

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."

like image 764
vikingosegundo Avatar asked Jun 06 '14 14:06

vikingosegundo


1 Answers

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.)

like image 69
Jean-Philippe Pellet Avatar answered Jan 01 '23 18:01

Jean-Philippe Pellet