0

I am displaying cards data from my model array, how do i randomize card data everytime i start the app and should not be repetitive. I am using ZLSwipeable view library for creating cards fro swipe feature. this is the function in which i have write all my code for swipe cards, and in the code wordsdata is the model array that i have declaredlike this var wordsdata = ModelRsult. CardView is the view i am displaying my cards into

screenshot of my card display screen

in the image it is shown banana as the first data, now when i run the app again it should show me another data first and so on

my source code:

func nextCardView() -> UIView?
    {
        let arrayData = wordsData
        if (self.colorIndex >= arrayData.count)
        {
            return nil
        }

        let cardView = CardView(frame: swipeableView.bounds)

        if loadCardsFromXib
        {
            let contentView = Bundle.main.loadNibNamed("CardContentView", owner: self, options: nil)?.first! as! CardView
            contentView.superVC = self
            contentView.translatesAutoresizingMaskIntoConstraints = false
            contentView.backgroundColor = cardView.backgroundColor

            cardView.addSubview(contentView)

            let data = arrayData[(self.colorIndex)] as ModelResult
            contentView.wordModel = data

            let savedValue = UserDefaults.standard.bool(forKey: "isLangChanged")

            tempLangChanged = savedValue

            if tempLangChanged == false
            {
                if let spanishName = data.cardWordEnglish, spanishName.count != 0
                {
                    contentView.wordSpanish.text = spanishName
                    print(spanishName)
                }
            }
            else
            {
                if let dict = data.cardWordImage, dict.count != 0
                {
                    let url = WS_wordIconUrl + dict
                    contentView.wordImage.kf.indicatorType = .activity
                    contentView.wordImage.kf.setImage(with: URL(string: url))
                }

                if let spanishName = data.cardWordSpanish, spanishName.count != 0
                {
                    contentView.wordSpanish.text = spanishName
                    print(spanishName)
                }
            }



            contentView.soundBtn.addTarget(self, action:#selector(self.soundBtn(sender:)), for: .touchUpInside)
            contentView.hideCard.addTarget(self, action:#selector(self.hideCard(sender:)), for: .touchUpInside)

            contentView.soundBtn.tag = self.colorIndex
            contentView.hideCard.tag = self.colorIndex

            cardView.tag = self.colorIndex
            constrain(contentView, cardView) { view1, view2 in
                view1.left == view2.left
                view1.top == view2.top
                view1.width == cardView.bounds.width
                view1.height == cardView.bounds.height
            }
        }
        colorIndex += 1
        return cardView

    }
  • Possible duplicate of [How to generate a random number in Swift without repeating the previous random number?](https://stackoverflow.com/questions/27541145/how-to-generate-a-random-number-in-swift-without-repeating-the-previous-random-n) – Joakim Danielson Jul 16 '19 at 17:51
  • have already tried this but doesnt work – nishant.narola Jul 16 '19 at 17:54
  • "when i run the app again it should show me another data", you need to store which card was shown in UserDefaults or similar and then when the app is re-opened you shuffle the cards until the first card is not the same as the one that you have stored. – Joakim Danielson Jul 17 '19 at 11:12
  • it worked sir...one more thing when i come to the cards display screen, i have used texttospeech to read out the word written on the card, but the problem is it is saying two times the first card along with the second card name, i want that it should speak the only name which card is shown, if i swipe next card, then it should speak next word – nishant.narola Jul 17 '19 at 11:19
  • Ask one question at a time – Joakim Danielson Jul 17 '19 at 11:24

1 Answers1

0

The best way for this problem is to shuffle the array where your cards are and then use it. In your case, I would shuffle it at the app start.

array.shuffle()
barbarity
  • 2,420
  • 1
  • 21
  • 29