0

I have two basic String Arrays which are being used to display Reptile / Mammal facts.

let reptiles = ["Chameleon", "Snake", "Turtle"]
let mammals = ["Dogs", "Cats", "Bears"]

There are two buttons for each. Mammal button & Reptile button. Users can click each to get a random fact.

func getRandomReptile() -> String {
    var randomNumber = Int(arc4random_uniform(UInt32(reptiles.count)))
    while previousNumber == randomNumber {
        randomNumber = Int(arc4random_uniform(UInt32(reptiles.count)))
    }
    previousNumber = randomNumber
    allArray.append(reptiles[randomNumber])
    return reptiles[randomNumber]
}

func getRandomMammal() -> String {
    var randomNumber = Int(arc4random_uniform(UInt32(mammals.count)))
    while previousNumber == randomNumber {
        randomNumber = Int(arc4random_uniform(UInt32(mammals.count)))
    }
    previousNumber = randomNumber
    allArray.append(mammals[randomNumber])
    return mammals[randomNumber]
}

What is the best way for a "Go Back" button to display the previous fact? It can be either a Reptile or Mammal fact, depending on what the user pressed.

I approached this by storing each of the Animals in an allArray Array using the append method. Then just using the removeLast method. This doesn't work cleanly, though, as the "back" button requires two taps. (one to remove, the other to display.) - I also don't think it's a very clean approach.

func getPrevious() -> String {
    if allArray.count > 1 {
        allArray.removeLast()
        return allArray.last! 
    }
    else {
        return allArray.last!
    }
}

What is the cleanest approach to "Go Back" on a combination of two random arrays?

4
  • 1
    Isn't it just the matter of saving previousNumber to a variable? And what do you mean by 'the best way' and the cleanest approach'? Commented Dec 19, 2016 at 7:37
  • @ElTomato - previousNumber is for checking against the same fact showing up multiple times in a row. If user tapped Mammals 10 times, then tapped Reptiles, then Back, they may get an Index out of range crash. - What I meant by "best way" and "cleanest" was looking for something like the append method. I find that "cleaner" than creating a third array and iterating over it. Commented Dec 19, 2016 at 7:48
  • A variable could be anything including an array. Commented Dec 19, 2016 at 7:53
  • can't you have two previousNumber variables and a boolean to check if reptile is tapped last or mammals button Commented Dec 19, 2016 at 17:36

1 Answer 1

1

save the selections in an array and add every new selection to that array. Walk back the array when you click previous button

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

2 Comments

This is the approach I am taking. Check my updated code in the original post. Having an isssue with the getPrevious function. I have the empty array check in there to prevent crashing which works fine. However - the "back" button takes two presses to update the array. Seems like the first press removes the last item in the array, then the second press updates the UI. Any tips on walking back the array that will update the UI in 1 tap? Thanks.
I was making a silly mistake. Inside the getPrevious method now, the button first removes the last item, then displays the last item, so long as the array count is greater than 1. Case closed :)

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.