我正在尝试在swift中编写以下Objective-C代码:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDate *startOfTheWeek;
NSTimeInterval interval;
[cal rangeOfUnit:NSWeekCalendarUnit
startDate:&startOfTheWeek
interval:&interval
forDate:now];
它会将本周开头写为startOfTheWeek
,将持续时间写为interval
。
在我写的操场上
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)"
虽然代码完成告诉我签名是
cal.rangeOfUnit(<#unit: NSCalendarUnit#>, startDate: AutoreleasingUnsafePointer<NSDate?>, interval: <#CMutablePointer<NSTimeInterval>#>, forDate: <#NSDate?#>)
发生错误Extra argument 'interval' in call
我做错了什么?
我也试过
cal.rangeOfUnit(NSCalendarUnit.WeekCalendarUnit, startDate: startDate, interval: duration, forDate: now)
但这会产生错误
&#34;找不到&range; rangeOfUnit&#39;接受提供的参数。&#34;
答案 0 :(得分:8)
试试这个:
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)
(默认情况下,第一个参数没有外部名称,因此不应命名。)