3

I'm trying to create a function in Xcode that I can call every time I hit a button to iterate through an array in sequence which then updates the value of the button title.

I can't seem to crack the challenge. I've tried various iterations of while loops and if statements but everytime I run it I end straight up at the last value in the array. Here's the code I've got at the moment, I tried to add a break clause to stop the function from automatically iterating through the whole array but it's now throwing up an error message saying that the code after the return statement will never be executed:

So, I've created an instance of a button within my viewController as follows:

@IBAction func repCount() {
    repCountButton.setTitle("\(repCounter.repCount())", forState: UIControlState.Normal)

I'm hoping that this will then update the title of the button with what I return from the repCount function that is called every time the button is pressed.

I've set up the function in a separate Swift file called repCounter and my code for the repCount function is as follows:

var repArray = [1,2,3,4,5]
var repArrayIndex: Int = 0

func repCount () -> String {
    if repArrayIndex < repArray.count {
        while repArrayIndex < repArray.count {
            return "\(repArray[repArrayIndex])"
            break
        }
        repArrayIndex++
    } else {
        return "\(repArray[0])"
    }
}

What I'd like this to do is to cycle through the array every time it is called and once it's got to the end of the array to start cycling from the beginning of the array again.

Thanks in advance for any help!

3
  • Are you just trying to get a string for the current index? Commented Feb 4, 2015 at 20:06
  • I think you should explain what you are trying to achieve. I don't know what you are trying to do, but note that you are incrementing the index outside of the while loop, is that what you want? And you never reset the index, is it also expected to go to the last element and stay there? Commented Feb 4, 2015 at 20:09
  • Hi, sorry - I've only been learning programming for a couple of days and this is my first post! I'll update my question with more context Commented Feb 4, 2015 at 20:28

2 Answers 2

1

I'm not at a computer where I can pull up XCode to test it, but I think the version below will do what you want. It isn't the most elegant code, but it is very straightforward. You have to do all the index juggling before the return statement since once the code hits a return, nothing following it will be executed.

I added some code to reset the index once it reaches the end of the array.

    var repArray = [1,2,3,4,5]
    var repArrayIndex: Int = 0

    func repCount () -> String {
        while repArrayIndex < repArray.count {
            var curIndex = repArrayIndex
            repArrayIndex = repArrayIndex + 1;
            if repArrayIndex >= repArray.count {
               repArrayIndex = 0
            }
            return "\(repArray[curIndex])"
        }
        return "\(repArray[0])"
    }
Sign up to request clarification or add additional context in comments.

4 Comments

For the record, this is not a good way to do this. Global variables and functions with side effects are generally a bad thing. Without the context of the rest of the code though, it's hard to point at a better solution.
Thanks Wayne, this looks like it's nearly there. This seems to iterate through the array nicely, but once you've called it 5 times (the array count) then it just returns the first value in the array. What I'd like to do is for it to continue cycling through the array.
Whats the goal string "12345" ??
I added code to make it continue to cycle by resetting the index.
1

Another option to getting this count, without iterating, is to do

// From Swift 1.2
func repCount () -> String {
    return count(repArray)
}

// Before Swift 1.2
func repCount () -> String {
    return countElements(repArray)
}

If you insist on iterating there are multiple options, see Iterating over an array, one which could be:

var count = 0
for rep in repArray {
   count++
}

Or you could for the round trip iteration provided in the other answer, but why do it the hard way, when you don't need to? Or is there something you haven't told us?

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.