gpt4 book ai didi

swift - 将参数传递给方法时出现错误 "extra argument in call"

转载 作者:搜寻专家 更新时间:2023-10-30 22:10:06 25 4
gpt4 key购买 nike

我正在尝试用 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

我在 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)"

虽然代码完成告诉我签名是

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)

但这会产生错误

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

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

关于swift - 将参数传递给方法时出现错误 "extra argument in call",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24084717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com