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 2.0 Structs as Data Models Finishing Up Our Model

Use of unresolved identifier 'GKRandomNumber'

Hi, i tried went over to this because there were some things in the just simple iPhone app with swift was outdated so i thought i would go over to swift 2.0

Now in the Rand function i am getting a really annoying error that keeps bugging me. I searched through Google after a solution but nothing that would solve my problem.

Is it because they swapped out the GKRandomNumber function or what?

If i wrote something wrong here's my code:

// // FactBook.swift // FunFacts // // Created by Mike Hansen on 03/05/2016. // Copyright © 2016 DelLong Company. All rights reserved. //

import Foundation

struct FactBook {

let factsArray = [
    "Ostriches can run faster than horses.",
    "Ants Stretch when they wake up in the morning.",
    "Olympic gold medals are actually mostly made out 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 the light from the sun to reach earth.",
    "Some bamboo plants can grow up to one meter in just one day!",
    "Some penguins can leap 2-3 meters out of the water.",
    "The state of Florida is bigger than england.",
    "On average, it takes 66 days to form a habit.",
    "Mammoths still walked earth when the great pyramid were being built."

    func getRandomFact() -> String {
        let randomNumber = GKRandomNumber.sharedRandom().nextIntWithUpperBound(10)


3 Answers

I believe you want to import GameplayKit and then you can generate a random number like this:

let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(10)

Why would i have to import GameplayKit ?

Well it has to be declared somewhere, so you'd have to import "something". Games use lots of random numbers, so it kinda makes sense. Anyway, hopefully my original answer was helpful.

Gavin your answer is correct. Mike, if you import GameKit the error should go away.

It did work i just didn't have time to give you best answer cause i'm in school but thank you! :)

Because that's where GKRandomSource is declared. Of course importing GameKit would work also, but that imports a whole bunch of additional stuff that you don't need just for a random number.

Oh i was just wondering why they didn't make the function a standalone instead of implementing a GameKit to my app i just thought it was weird

Oh i was just wondering why they didn't make the function a standalone instead of implementing a GameKit to my app i just thought it was weird