2

I am Currently working on a swift project in XCode, and I've encountered an issue where I cannot increment an array's one element by through various endeavours.

My ViewController.Swift

import UIKit

class FailureViewController: UIViewController {

    @IBOutlet weak var failFactLabel: UILabel!

    let failFact = Failure()

    override func viewDidLoad() {
        super.viewDidLoad()

        failFactLabel.text = failFact.failArray[0]
    }

   @IBAction func showFunFact() {   
        //this is where the issue is
    }
}

For the function showFunFact I have previously tried

for var x = 0; x < failArray.cout; x++ {
    failFactLabel.text = failFact.failArray[x]
}

but encountered the error: "Use of Unresolved Identifier".
Putting this aside, I decided to use

for var x = 0; x < 10; {
       x+=1
} 

Although this does not generate an error however, I see it stops at the first element in my array. I tried +=5 and it displays the 5th element in my array once, I believe this code runs through once. I need it to be a consistently working piece so it continuously displays the next element, I am stumped because this should theoretically work since it is called a "Loop" Any help or suggestion is Appreciated!

Objective Redefined: I am trying to show one fact at a time, before this I have used a random function to generate a random fact every time however things tend to get repetitive and therefore I decided to start on the initial array index is 0 and grab the next fact (next element in array) every time the button is pressed (Thanks to Luk's Question)

4
  • 1
    well, what exactly are you trying to achieve? show only one fact at a time, a different one each touch or change through the facts rapidly automatically? Commented Jul 14, 2015 at 10:33
  • I am trying to show one fact at a time, before this I have used a random function to generate a random fact every time however things tend to get repetitive and therefore I decided to start on the initial array index is 0 and grab the next fact (next element in array) every time the button is pressed Commented Jul 14, 2015 at 10:36
  • You are using failArray in one place, failFact.failArray in another. That seems wrong. Commented Jul 14, 2015 at 10:38
  • @gnasher729 I see your logic. What do you suggest I should do? Commented Jul 14, 2015 at 10:39

1 Answer 1

3

You need to create an instance variable to hold the current factIndex, if that is what you actually are trying:

import UIKit

class FailureViewController: UIViewController {

    @IBOutlet weak var failFactLabel: UILabel!
    var factIndex = 0

    let failFact = Failure()

    override func viewDidLoad() {
        super.viewDidLoad()

        failFactLabel.text = failFact.failArray[factIndex]
    }

   @IBAction func showFunFact() {
        factIndex++
        if factIndex >= failFact.failArray.count then {
            factIndex = 0
        }
        failFactLabel.text = failFact.failArray[factIndex]
    }
}
Sign up to request clarification or add additional context in comments.

14 Comments

Thanks for the reply! Should I directly implement this code or should I put this into a for loop or if loop to put this into constraints?
@HenryLi that code should work as is. As I am not 100% sure what you are actually trying to achieve, try it out and then come back and tell me if that is what you desired or try to explain what should happen instead.
@HenryLi I already added/delcared the factIndex for you - I am guessing you added it inside showFunFact - please take a closer look at my solution, I added the factIndext in the 6th line. It has to have class/object level scope and has to be defined outside the method.
works like a charm! Do you suggest adding an if statement like if(factindex<failarray.count) {failFactLabel.text = failFact.failArray[factIndex] }
My mistake previously, I am a clumsy person.
|

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.