Swift 3 calendar.range and issues with Date

I just tried converting my project to Swift 3 in the Xcode 8 beta 1 release. Most things worked - but this one I have no idea how to get it to work.


Here's a snippet I took from a playground:


let calendar = Calendar.current()

var interval = TimeInterval(0)

var startOfTheWeek: Date = Date()

let startTime = Date()

calendar.range(of: .weekOfMonth, start: &startOfTheWeek, interval: &interval, for: startTime)


The compiler complains that

Cannot pass immutable value of type 'NSDate?' as inout argument


It’s complaining on

startOfTheWeek
which is mutable...


Any thoughts?

Accepted Reply

Your declaration of

startOfTheWeek
should look like this:
var startOfTheWeek: NSDate? = NSDate()

Swift’s API importer takes a ‘hands off’ approach when dealing with parameters that are passed by pointer, so the

start:
parameter of
range(of:start:interval:for:)
is of type
AutoreleasingUnsafeMutablePointer<NSDate?>?
. The
&
brings that back to
NSDate?
, which is how you need to declare
startOfTheWeek
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

Your declaration of

startOfTheWeek
should look like this:
var startOfTheWeek: NSDate? = NSDate()

Swift’s API importer takes a ‘hands off’ approach when dealing with parameters that are passed by pointer, so the

start:
parameter of
range(of:start:interval:for:)
is of type
AutoreleasingUnsafeMutablePointer<NSDate?>?
. The
&
brings that back to
NSDate?
, which is how you need to declare
startOfTheWeek
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks! I think I met you at WWDC with a Swift question - you rock!