1

hi im new to SO so i hope i make everything right.... :) i started some Days ago iOS programming and no i have a problem that drives me crazy!

Here is the code of the class FactBook:

    import Foundation
import UIKit

public class FactBook {

    var randomNumberForAll: Int!

    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.",
        "You can finish the entire FunFacts iOS app course in the time it takes to set up the emulator for the Android course"
    ]

    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 randomFact() ->String {
        //Count
        var arrayCount =  UInt32(factsArray.count)
        //Random Number from count
        var unsignedRandomNumber = arc4random_uniform(arrayCount)
        //Random Number as Int
        var randomNumber = Int(unsignedRandomNumber)

        self.randomNumberForAll = randomNumber

        return factsArray[randomNumber]
    }

    func randomImage() -> UIImage {
        var imageRandom = randomNumberForAll

        return imagesArray[imageRandom]!
    }
}

Here is the ViewController:

import UIKit

class ViewController: UIViewController {

//Lable, Structs etc.----------------------------------NEU----------------------------
@IBOutlet weak var funFactLable: UILabel!
@IBOutlet weak var funFactButton: UIButton!
@IBOutlet weak var imageView: UIImageView!

let colorWheel = ColorWheel()

var counter = 0

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //Loads first Label Text
    funFactLable.text = "An interestig Fun Fact comes with one push!"
    funFactButton.setTitle("For the first Fun Fact - Push!", forState: UIControlState.Normal)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//ACTIONS-------------------------------------------------------------------------------------
@IBAction func showFunFact() {
    var randomColor = colorWheel.randomColor()
    var randomImage = FactBook.randomImage()
    var randomFact = FactBook.randomFact()

    counter++

    //BackgroundColor change
    view.backgroundColor = randomColor
    //Button-TextColor change
    funFactButton.tintColor = randomColor
    //Button-Title = Counter + Text
    funFactButton.setTitle("\(counter). Fun Fact", forState: UIControlState.Normal)

    //fadeIn fadeOut FactLable
    self.funFactLable.fadeOut(completion: {
        (finished: Bool) -> Void in
        self.funFactLable.text = randomFact

        self.funFactLable.fadeIn()
    })

    //fadeIn fadeOut ImageView
    self.imageView.fadeOut(completion: {
        (finished: Bool) -> Void in
        self.imageView.image = randomImage

        self.imageView.fadeIn()
    })
}
}

so the problem is, i cant call the functions (randomFact and randomImage) in the ViewController.

But always the Error Missing argument for parameter #1 in call comes up. When i change the class factbook to struct i can call the func“s but then the self.randomNumberForAll doesnt work.

What i just want is to use the randomnumber from the func randomFact() in the func randomImage()....

Thank you if u can help me :) !!!!

Wendi0815
  • 11
  • 1
  • 3

2 Answers2

0

I hope this isn't a case of the blind trying to lead the blind as I'm new to swift, but I took this course too recently and was playing around with the code similarly to you. Seems like loading in the Factbook like you did Colorwheel should eliminate your missing parameter error. Unlike the colorwheel though, maybe it has to be a variable instead of a constant, so that you can store and change your randomFactForAll:

let colorWheel = ColorWheel() var factBook = FactBook()

little late, hope this helps

dhall
  • 1
  • 1
0

Need to add keyword "class" to your functions randomFact() and randomImage() to make them as class functions.

Please check the following question for similar issue.

Community
  • 1
  • 1
Thiru
  • 1,380
  • 13
  • 21