swift – 将参数传递给方法时出现“调用中的额外参数”错误

前端之家收集整理的这篇文章主要介绍了swift – 将参数传递给方法时出现“调用中的额外参数”错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在 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.”

试试这个:
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)

(默认情况下,第一个参数没有外部名称,因此不应该命名.)

原文链接:https://www.f2er.com/swift/318605.html

猜你在找的Swift相关文章