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

Link a string with an image in a Array

Hi!

I have a Question about strings and images in iOS.

So i start to write my first iPhone App with the Tutorial "Build a Simple iPhone App with Swift" and play a bit around...

So what i want now is to link the String-Fact ("Ants stretch when they wake up in the morning.") with an image of Ants. So every time this Fact comes up the image of an Ant comes in the image View.

Like this: Imgur

So now i have 2 Arrays, 1 with the text and 1 with the images for each Text-String. Each one has this randomNumber func, so up to now i have to random Facts -> random images. So no image fit to the Text......

struct FactBook {
    let factsArray = [
        "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{
        //Count
        var unsignedArrayCount =  UInt32(factsArray.count)
        //Random Number from count
        var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
        //Random Number as Int
        var randomNumber = Int(unsignedRandomNumber)

        return factsArray[randomNumber]
    }
}

struct Images {
    let imagesArray = [
        UIImage(named: "ants.jpg"),
        UIImage(named: "ostriches.jpg"),
        UIImage(named: "olymp.jpg"),
        UIImage(named: "bones.jpg"),
        UIImage(named: "light.jpg"),
        UIImage(named: "bamboo.jpg"),
        UIImage(named: "florida.jpg"),
        UIImage(named: "penguins.jpg"),
        UIImage(named: "habit.jpg"),
        UIImage(named: "mammoth.jpg")
    ]

    func randomImage() -> UIImage {
        //Count
        var unsignedArrayCount =  UInt32(imagesArray.count)
        //Random Number from count
        var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
        //Random Number as Int
        var randomNumber = Int(unsignedRandomNumber)

        return imagesArray[randomNumber]
    }
}```

I hope you can understand me and have an idea of what i want ;-)

If u need something, im glad to help!

BIG THANKS for now!

2 Answers

Daniel,

Just use the same index to call them out of the array.

For example say your random number is 3.

var randomNumber = 3

Then

var someText = factsArray[randomNumber]

var imageSource = imagesArray[randomNumber]

Hey Jeremy Hayden ,

that was also my thought, but its not that easy.... Or i don't see it....

Cause i can't call the randomNumber from func randomFact in the struct Images, the struct Images don't know the randomNumber from func randomFacts

So for an idea where i have to put this in, i would be happy! It drives me crazy right now........

BIG Thanks!

Pull the random number function out of your image and fact function.

Run it separately. After you establish your number, then you can call the fact and image directly from the array, or pass the number to each function

You don't need 2 random numbers, just one.

I didn't work cause if i put the function out of the struct

  1. i can't count the array cause the function don't know the array - and i want it variable so if i add more facts it should edit the count number.

  2. the function will run just one time if the app starts cause the function is not linked in the button action.

I know what u mean and for u its simple but i tried every ***** way..... it doesn't work! Im really sad and crazy right now.....