Why would this be "ambiguous"?

26 views
Skip to first unread message

Alex Hall

unread,
Oct 20, 2015, 4:32:11 PM10/20/15
to swift-l...@googlegroups.com
Hello all,Calendar, NSDate, etc. This line:
let differenceComponents:NSDateComponents = calendar.components(.Year | .month | .Week | .Day | .Hour | .Minute, fromDate:date, toDate:nowDate, options:0)
gives the error:
type of statement is ambiguous without more context

I'm wondering why it would be, and what I can do to fix it? The function being called returns NSDateComponents, which I assign to my variable. I'm not seeing the ambiguity, and Google has nothing to say on the topic that relates to this. I just found a couple instances where the person either had to change a function or do something different with the options. Thanks.

Sorry if this isn't the list for this. I have an iOS app and I'm just starting to explore NS

--
Have a great day,
Alex Hall
meh...@icloud.com

Brent Royal-Gordon

unread,
Oct 20, 2015, 4:37:46 PM10/20/15
to Alex Hall, swift-l...@googlegroups.com
> let differenceComponents:NSDateComponents = calendar.components(.Year | .month | .Week | .Day | .Hour | .Minute, fromDate:date, toDate:nowDate, options:0)

If you’re using Swift 2, you now need to write that set of calendar units as [.Year, .Month, .Week, .Day, .Hour, .Minute] instead of trying to or them together. Not sure that will completely fix your problem, but it’s a start.

--
Brent Royal-Gordon
Architechies

Daniel Tartaglia

unread,
Oct 20, 2015, 4:49:22 PM10/20/15
to swift-l...@googlegroups.com
This works:

let differenceComponents = calendar.components([.Year, .Month, .Day, .Hour, .Minute],
fromDate: date,
toDate: nowDate,
options: NSCalendarOptions())


--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/F0FEF52E-A258-44CD-AF07-021C3928E134%40icloud.com.
For more options, visit https://groups.google.com/d/optout.

Kevin Ballard

unread,
Oct 20, 2015, 4:49:58 PM10/20/15
to swift-l...@googlegroups.com
You also need to specify the options as `[]` instead of as `0`. If
you're still on Swift 1.2, then you still need to specify options as
`nil` instead of `0`.

You also typed `.month` instead of `.Month`.

-Kevin Ballard

Kevin Ballard

unread,
Oct 20, 2015, 4:50:11 PM10/20/15
to swift-l...@googlegroups.com
The `NSCalendarOptions()` is unnecessarily verbose. Yeah that works, but `[]` is much shorter and more idiomatic.
 
-Kevin

Alex Hall

unread,
Oct 20, 2015, 5:11:15 PM10/20/15
to Kevin Ballard, swift-l...@googlegroups.com
Thank you, everyone. I apparently missed the memo regarding or-ing things together in Swift 2; everything now works as expected. Is this specific to certain Cocoa classes, or is it safe to assume that anytime an API in Cocoa wants or-ed items, I should pass them in as an array?

For more options, visit https://groups.google.com/d/optout.


--

Jim Dovey

unread,
Oct 20, 2015, 5:18:22 PM10/20/15
to Alex Hall, Kevin Ballard, swift-l...@googlegroups.com
> On Oct 20, 2015, at 2:11 PM, Alex Hall <meh...@icloud.com> wrote:
>
> Thank you, everyone. I apparently missed the memo regarding or-ing things together in Swift 2; everything now works as expected. Is this specific to certain Cocoa classes, or is it safe to assume that anytime an API in Cocoa wants or-ed items, I should pass them in as an array?

This is the new syntax for OptionSet types, which is a part of the standard library in Swift 2.0. When converting from ObjC, any type declared using the NS_OPTIONS or CF_OPTIONS macros are imported as OptionSets, so they all get this syntax.

Cheers,
-Jim

Reply all
Reply to author
Forward
0 new messages