Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Simple iPhone App with Swift Structs As Data Models Finishing Up Our Model

Mary Kenkel
Mary Kenkel
4,028 Points

GameKit Vs GamePlayKit: Build a Simple iPhone App with Swift 3>Structs As Data Models> Finishing Up Our Model

Hello,

I just finished watching the video at Build a Simple iPhone App with Swift 3>Structs As Data Models> Finishing Up Our Model and I was trying to do some research on some of the coding that Pasan does in the video.

He creates a random number by first importing GameKit and then executing the GKRandomSource function to generate the random number.

But when I look at the documentation for GameKit, I don't see the GKRandomSource function listed as included. See https://developer.apple.com/reference/gamekit

However, I can find GKRandomSource included as a function within the GameplayKit framework. In fact, there's a whole section for randomization in GameplayKit. See https://developer.apple.com/reference/gameplaykit

So how does Pasan's coding work if it's importing a framework that doesn't include the function that he uses?

And what's the deal with GameKit Vs GamePlayKit?

Thanks in advance for any help understanding this...

Mary

BTW, here's the code that Pasan uses:

import GameKit

struct FactProvider {
    let facts = [
        "Ants stretch when they wake up in the morning.",
        "Ostriches can run faster than horses.",
        "Olympic gold medals are actually made mostly of silver.",
        "You are born with 300 bones; by the time you are an adult you will have 206.",
        "It takes about 8 minutes for light from the Sun to reach Earth.",
        "Some bamboo plants can grow almost a meter in just one day.",
        "The state of Florida is bigger than England.",
        "Some penguins can leap 2-3 meters out of the water.",
        "On average, it takes 66 days to form a new habit.",
        "Mammoths still walked the Earth when the Great Pyramid was being built."
    ]

    func randomFact() -> String {
        let randomNumber = GKRandomSource.sharedRandom().nextInt(upperBound: 9)

        return facts[randomNumber]
    }

}

Thanks again!

2 Answers

Dan Lindsay
Dan Lindsay
39,611 Points

Hi Mary,

If you command-click on GameKit, you will see that it imports GameplayKit, so it can tap into all that GameplayKit can do. GameKit imports all the bigger graphics kits like Metal, SceneKit and SpriteKit, which GameplayKit does not, so it looks like you can do a lot more with GameKit. Not sure why he used GameKit instead of GameplayKit though, as this app doesn’t require any high-end graphics.

Hope this helps!

Dan

Mary Kenkel
Mary Kenkel
4,028 Points

Dan!

Thank you so much for the answer! That helps a lot!

Thanks again....

Mary