GameplayKit - how do you set up GKLinearCongruentialRandomSource

I have been trying to play with GameplayKit's GKLinearCongruentialRandomSource function with a playground and I can't seem to get anything useful out of it. I am new to Swift for sure, so it is most-likely a bonehead error. :-) However I couldent find an example online and the docs don't really show (at least to me) any functional code examples.


Here is what I have in a playground:

//: testing out new GameplayKit (GK) Congruential Random Source 
import UIKit 
import GameplayKit 
let seed: UInt64 = 987234904 
let random = GKLinearCongruentialRandomSource(seed: seed) 
let result = random.nextIntWithUpperBound(10)  // should be a random Int from 0 to 9 (total 10 possibilities


Normally playground will give me the 'values' as I assign / use them. However for this example it did not. If I comment out the 'let random' and 'let result' lines, then playground shows the seed was registed by showing me it's value on the right-side. That makes me think the on-the-fly compiler is choking on the 'let random' statement.


The funny thing is Playground is not kicking out any syntax errors.


So what silly thing am I doing wrong? Can somebody give me a quick sample on how to actually use this cool new feature?

Okay - I think I have identified what the actual issue is. Playgrounds inherently hide the 'debug' area so I did not see the error code that was being generated.

Here is the error:

Playground execution failed: /var/folders/_9/sqdvh1m923j0ts4t7sldpyvr0000gn/T/./lldb/10162/playground861.swift:22:14: error: 'GKLinearCongruentialRandomSource' is only available on iOS 9.0 or newer
let random = GKLinearCongruentialRandomSource(seed: seed)
            ^
/var/folders/_9/sqdvh1m923j0ts4t7sldpyvr0000gn/T/./lldb/10162/playground861.swift:22:14: note: guard with version check
let random = GKLinearCongruentialRandomSource(seed: seed)

So the real issue is my Xcode 7 beta playground does not appear to be recognized as IOS 9. Is there a way to set Playground's IOS level? I have verified in the File Inspecter that this playground is "IOS" and not "OS X" - but I can't find a way to change the IOS version.

Accepted Answer

OKay - apparently there is a 'bug' in Playground for this issue. Here is how you get around it.


import UIKit
import GameplayKit
if #available (iOS 9,*)
{
    let seed: UInt64 = 987234904
    let random = GKLinearCongruentialRandomSource(seed: seed)
    let result = random.nextIntWithUpperBound(10)  /
}


Notice the "if #available (iOS 9, *)" portion. If you dont put this in your playground, it will error out with the above mentioned error code.


The funny thing is - if you do put that condtional in your playground, you will get a 'warning' stating:

Unnecessary check for 'iOS': minimum deployment target ensures guard will always be true

It still works so no harm - no foul while Apple sorts out Playground's Bipolar issue. :-)

What is the deployment target setting for that test app?

Same issue, deployment target set to 9.0

GameplayKit - how do you set up GKLinearCongruentialRandomSource
 
 
Q