0

I have a timer which starts when you press a button.

A random number is generated and the time should stop when it reaches the random number (from 1-100).

The only way I can get it work is by generating the random number in the time function (counting), however this generates a random number every time the counter increases, which means it will only stop if the random number generates the same as the counter. If the random number and the time do not fall on the same number, the time goes above 100 - I found this by putting in a label to show the random number.

I have managed to get the random number to generate just once by putting it in the button function, however then the timer function does not recognise stopNumber. this way seems to be the most practical, how do I call the Variable stopNumber from the button function in the counting function?

code below:

func generateNewNumber() -> UInt32 {
    return arc4random_uniform(100) + 1

}


func Counting() {

var stopNumber:Int = Int(generateNewNumber())

timerCount += 1
timerLabel.text = "\(timerCount)"

if timerCount == stopNumber {
        timer.invalidate()
        timerRunning = false
}

@IBAction func startButton(sender: UIButton) {


    if timerRunning == false && timerCount == 0 {
        timer = NSTimer.scheduledTimerWithTimeInterval(0.06, target: self, selector: Selector("Counting"), userInfo: nil, repeats: true)
        timerRunning = true
        }

}

I am fairly new to this any help would be really appreciated.

1
  • Why not generate the random number in startButton, pass the value as an argument to counting? Commented May 14, 2015 at 19:05

4 Answers 4

1

There are several ways you can share variables.

  1. create a variable outside the method, doing so:

    class myClass {
        var myVariable = 0
        myMethod() {/*use variable here*/}
        anotherMethod() {/*use variable here too*/}
    }
    
  2. you can create the variable outside the class which will even make it accessible for other classes:

    var myVariable = 0
    
    class myClass {
        myMethod() {/*use variable here*/}
        anotherMethod() {/*use variable here too*/}
    }
    
  3. you could pass the variable as parameter when calling a method:

    myMethod() {
        var variable = 0
        anotherMethod(variable) {/*use variable here*/}
    }
    anotherMethod(var variab) {/*use 'variab' here*/}
    

    this is rather inconvenient which is why I prefer to use one of the others.

I hope that helps you :)

Sign up to request clarification or add additional context in comments.

Comments

0

Declare your variable outside of the function's scope but insides the class' scope. The following code snippet shows the concept of this. If you need help implementing it into your code, try harder :) If it still does not work, ask again.

class myClass{
  private var stopNumber:Int = 15
  func counting() {
    stopNumber++ 
    //You can access this variable from within all functions inside of myClass
  }
}

Comments

0

Thanks for all the input, the issue I had with some of the above was declaring the variable outside the function resulted in the random number being generated when the App Launched. I wanted a new Random number to generate everytime the start button is pressed.

I figured out a solution to this below

var stopNumber = 0


}

func Counting() {

timerCount += 1
timerLabel.text = "\(timerCount)"

if timerCount == stopNumber {
    timer.invalidate()
    timerRunning = false
}

@IBAction func startButton(sender: UIButton) {

var stopGen:Int = Int (arc4random_uniform(100) + 1)
stopNumber = stopGen

if timerRunning == false && timerCount == 0 {
    timer = NSTimer.scheduledTimerWithTimeInterval(0.06, target: self, selector: Selector("Counting"), userInfo: nil, repeats: true)
    timerRunning = true
    }

}

Thanks for everyones help

Comments

0
let randomNumber:Float = arc4random_uniform(2) == 0 ? -1.0 :  1.0
  • the 2 is the max number
  • the 0 is the fi value, that means if the randomnumber is 0 the value is -1 you can change it of course
  • the last value is if the value isn't 0
  • you can then use these values to say weather it is true or not

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.