3

My intent is to use nextInt(upperBound: Int)

let randomInt: (Int) -> Int = GKRandomSource.sharedRandom().nextInt
var myNumber = randomInt(10)

Produce error: Playground execution failed: error: ambiguous reference to member 'nextInt()'

Why?

Swift 3, xCode 8 beta 6

Atsuo Sakakihara
  • 179
  • 1
  • 3
  • 12
  • Compiles fine for me – maybe try cleaning your build folder? You could also try disambiguating by referring to the method as `GKRandomSource.sharedRandom().nextInt(upperBound:)` – Hamish Sep 04 '16 at 18:14
  • I'm playing with Playground. This error popped up in Debug area on bottom. – Atsuo Sakakihara Sep 04 '16 at 18:38
  • Playgrounds are notoriously buggy – try in a full project – Hamish Sep 04 '16 at 18:41
  • It compiles and works for me in an Xcode 8 beta 6 Playground. Do you import something else than GameKit? – Eric Aya Sep 04 '16 at 18:41
  • @EricAya UIKit and GameplayKit – Atsuo Sakakihara Sep 04 '16 at 18:43
  • Can't reproduce any error. Voting to close. – matt Sep 04 '16 at 18:44
  • Huh... I *can* reproduce your problem in an Xcode 8 beta 6 playground – and for some reason changing the method type to `(UInt) -> UInt` allows it to compile and run (although Xcode still generates an inline error). You should probably file a bug report with Apple. – Hamish Sep 04 '16 at 18:45
  • @Hamish you have me there. But I still can't reproduce in an app. – matt Sep 04 '16 at 19:06
  • @matt I also cannot reproduce it in a full project (I assume that's what you mean by "in an app"), but in a playground ([my exact playground](https://infinit.io/_/jg8perb)), the console will generate an error of "*ambiguous reference to member 'nextInt()'*" (although no inline errors will be generated). The two candidates the compiler find are `open func nextInt() -> Int` & `open func nextInt(upperBound: UInt) -> UInt` (I have no idea where the `UInt` comes from). Something weird is definitely going on here – I certainly agree with you that playgrounds are the work of the devil! – Hamish Sep 04 '16 at 19:22

1 Answers1

1

Your code compiles fine in a real iOS app, as shown by this screen shot:

enter image description here

That, an actual iOS app project, is the real test of whether code is valid. Playgrounds do not represent real Swift environments.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 2
    Actually, I regard playgrounds as the work of the devil and I wish they had never been invented. But that's another story. (Actually, it's the same story.) – matt Sep 04 '16 at 18:47