我试图在
swift中编写以下Objective-C代码:
NSCalendar *cal = [NSCalendar currentCalendar]; NSDate *now = [NSDate date]; NSDate *startOfTheWeek; NSTimeInterval interval; [cal rangeOfUnit:NSWeekCalendarUnit startDate:&startOfTheWeek interval:&interval forDate:now];
它会写本周开始的时候开始的周期和周期间隔.
在我写的操场上
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?#>)
在调用中说出额外参数’interval’时出错
我究竟做错了什么?
我也试过了
cal.rangeOfUnit(NSCalendarUnit.WeekCalendarUnit,forDate: now)
但这会产生错误
“could not find an overload for ‘rangeOfUnit’ that accepts the supplied arguments.”
试试这个:
原文链接:https://www.f2er.com/swift/318605.htmllet now = NSDate() var startDate: NSDate? = nil var duration: NSTimeInterval = 0 let cal = NSCalendar.currentCalendar() cal.rangeOfUnit(NSCalendarUnit.WeekCalendarUnit,startDate: &startDate,interval: &duration,forDate: now)
(默认情况下,第一个参数没有外部名称,因此不应该命名.)