gpt4 book ai didi

swift - 如何从模型数组中随机化卡片数据而不重复

转载 作者:行者123 更新时间:2023-11-30 10:39:52 26 4
gpt4 key购买 nike

我正在显示模型数组中的卡片数据,如何在每次启动应用程序时随机化卡片数据并且不应重复。我正在使用 ZLSwipeable View 库来创建滑动功能的卡片。这是我编写了所有刷卡代码的函数,在代码中,wordsdata 是我声明的模型数组,如下所示 var Wordsdata = ModelRsult.CardView 是我将卡片显示到的 View

screenshot of my card display screen

在图像中,它显示香蕉作为第一个数据,现在当我再次运行应用程序时,它应该首先显示另一个数据,依此类推

我的源代码:

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

}

最佳答案

解决这个问题的最好方法是对你的牌所在的数组进行洗牌,然后使用它。就您而言,我会在应用程序启动时对其进行随机播放。

array.shuffle()

关于swift - 如何从模型数组中随机化卡片数据而不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57062878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com