i cant seem to see if a word is picked from a array and set a respectful image beside it in swift2

i cant seem to see if a word is picked from a array and set a respectful image beside it in swift2

im 9 years old so its kind of hard for me

Hello,


can you explain and maybe show some of your code ?


You want to pick a word from an array, right ?


So you have declared an array, like


var words = ["Cat", "Dog", "Cow"]

to pick a word, just ask with its number.

Take care, it starts at zero and here ends at 2

For instance:

let myWord = word[0]

will give you "Cat"


I understand you want to display an image corresponding to the word you selected ?


If so:

- you have defined a viewController.

- In Interface Builder, you should insert an UIImageView in the view

declare an IBOutlet in ViewController

@IBOutlet var wordImage : UIImageView!

- connect the object in the view to this IBOutlet in code


Now, you should explain how user select the word ?

Do you create a button for each word ?

Do you select in a picker.

Another solution ?


In anycase, you have to put images (png format) in your project. Call them Cat.png, Dog.png, Cow.png

The names must be exactly the same as the names in words array.

When you select a word, you will be able to display the image with :

wordImage.image = UIImage(named: myWord)


Once you have told, tell if you need more help.

The user clicks the button and it picks out a random word from the array using the ark 4 random

OK, that's a good solution.


So, in the IBAction of the button, you'll have :


let numberOfImages = words.count

let selection = Int(arc4random_uniform(UInt32(numberOfImages)))

let selectedWord = words[selection]

// Now you can set the image

wordImage.image = UIImage(named: selectedWord)


// Remember, you must have dragged the images into the XCode project

i also do have to add multiple images like if i got soccer from the array the image would have to be a soccer ball

my code is this


/
/
/
/
/
/
/ 
import UIKit
import AVFoundation
/ 
var audioPlayer = AVAudioPlayer()
class ViewController: UIViewController {
     /
    @IBOutlet weak var duckSlidein: UIImageView!
   
    @IBOutlet weak var animationButton: UIButton!
   
    @IBOutlet weak var duck: UIImageView!
   
    @IBOutlet weak var boredText: UILabel!
   
    @IBOutlet weak var boredButton: UIButton!
   
   
    /
    var todos = ["read a book", "play some battle ships", "draw something", "play tag", "go to the mall", "finish of work", "watch some cat videos", "eat something", "write a poem", "play with your friends", "play some soccer", "search something up", "do some origami", "play with your dog", "go for a walk", "call your friends", "go for a quick run", "do some singing", "think where you will be in 20 years", "play some chess", "play handball", "watch a movie", "listen to music", "watch some youtube", "play with toys", "help your parents", "clean your room or desk", "sit in the tub", "do some coloring", "clean the house", "go out side", "do some gardening", "do the cooking", "Plant a tree", "Bake cookies for the neighbors -- or your family","Explore nature and go geocaching", "Explore nature and go geocaching", "Make lanyard key chains", "Visit an interactive museum", "Have a catch in a local park or the backyard", "Build an indoor tent or fort with couches and some bed sheets", "Head to the local library", "dance to some music", "Put together a jigsaw puzzle", "Create papier mch", "Make homemade play dough", "Follow a no-bake recipe", "Play DIY bowling: Spray-paint two-liter bottles as makeshift bowling pins and use a handy soccer ball in place of a bowling ball", "Plan a scavenger hunt", "String together macaroni for jewelry", "Host a picnic lunch indoors or outdoor", "Learn how to tie-dye T-shirts", "Pull out the dress box, put on funny clothes and have a photo shoot", "Head outdoors for bird watching", "Create a time capsule", "Write letters to soldiers", "Play balloon volleyball", "Have a potato-sack race", "Arrange a Jenga tournament", "Camp out in the backyard", "Make up a secret handshake", "Count how many times you can spin in a circle without getting dizzy", "Make fruit kabobs", "see what you can make with lego", "Draw murals with sidewalk chalk", "Play lily-pad leap with small rugs and towels strategically placed throughout the playroom or living room", "Construct an indoor obstacle course", "Choreograph a dance routine", "Host a movie night. Plan for it by drawing tickets, making popcorn and setting up the room like a theater", "Make bath fizzies by combining citric acid, baking soda and cornstarch", "Pull out some old socks and draw faces on them to make sock puppets -- then put on a show", "Exercise those finger-painting skills", "Pull out Twister", "Practice saying the 50 states in alphabetical order", "Host an at-home field day, complete with tug of war and team colors", "Host an at-home field day, complete with tug of war and team colors", "Experiment with science. Make a papier mch volcano and mix baking soda and vinegar for an explosive reaction!", "Learn a new card game", "Host a tea party", "Partake in brainteasers, such as Sudoku, crossword puzzles and word searches", "Set up the basketball game of HORSE or just shoot hoops", "Jump rope", "Go on a walk and let kids take pictures (with a camera) of their favorite stuff around the neighborhood", "Break out the pots and pans and practice drumming skills (ear plugs required for parents!)", "Rearrange your child's bedroom", "practice pencil drumming", "Find and write to a pen pal", "Mix homemade bubbles: 1 cup granulated soap or soap powder, 1 quart warm water, and liquid food coloring", "Make sandwiches and deliver them to a homeless shelter or the local food pantry", "Set up a platter of new foods, maturing your tykes palette", "Sew a pillow. Just grab some fabric, scissors, stuffing and a needle"]
    var whichTodoToChoose = 0
   
    /
   
    var ButtonText = ["Im Still Bored"]
    var whichButtonTextToChoose = 0
   
    override func viewDidLoad() {
        super.viewDidLoad()
       
        duckSlidein.center.x = self.view.frame.width + 30
       
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 5, options: [], animations: ({
           
            self.duckSlidein.center.x = self.view.frame.width  / 2
       
        }), completion: nil)
       
        animationButton.center.x = self.view.frame.width + 30
       
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 5.0, options: [], animations: ({
           
        }), completion: nil)
       
        self.animationButton.center.x = self.view.frame.width / 2
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
    @IBAction func BoredButtonTapped(sender: UIButton) {
       chooseAQuote()
        chooseTextOnButton()
    }
   
    func chooseAQuote() {
        showTheQuote()
        whichTodoToChoose = Int(arc4random_uniform(90))
    }
   
    func showTheQuote() {
        boredText.text = "\(todos[whichTodoToChoose])"
    }
   
    func chooseTextOnButton() {
        boredButton.setTitle("\(ButtonText[whichButtonTextToChoose])", forState: UIControlState.Normal)
    }
}

Unless you reuse the func elsewhere, you should avoid creating a function for each instruction.


It is valid code but it is much more difficult to read.


So, the action starts when you tap on boredButton


    @IBAction func BoredButtonTapped(sender: UIButton) {
    
        boredText.text = "\(todos[whichTodoToChoose])"    //     showTheQuote()             
        whichTodoToChoose = Int(arc4random_uniform(90))          //  chooseAQuote()
        boredButton.setTitle("\(ButtonText[whichButtonTextToChoose])", forState: UIControlState.Normal)      //    chooseTextOnButton()
    }


But you do not load any image here, you just set buttons title.

In addition, you show the quote before you choose the quote. Is it what you want ?


You do not set any image. Is it done elsewhere in code ?

i cant seem to see if a word is picked from a array and set a respectful image beside it in swift2
 
 
Q