gpt4 book ai didi

swift - 从随机排列的数组中设置按钮的标题 - Swift

转载 作者:行者123 更新时间:2023-11-30 12:08:14 25 4
gpt4 key购买 nike

这是我的代码

    func shuffle() {
let footballClubsInPoland = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: self.footballClubsInPoland)
option1.setTitle((footballClubsInPoland[0] as AnyObject).name, for: .normal)
option2.setTitle((footballClubsInPoland[1] as AnyObject).name, for: .normal)
print(footballClubsInPoland[0])
print(footballClubsInPoland[1])
}

我的问题是,我可以在控制台中看到已打乱顺序的对象,但按钮的标题未在模拟器中加载。我该怎么办?

最佳答案

在执行 (footballClubsInPoland[0] as AnyObject 时,您将元素转换为 AnyObject,而在调用 .name 时,您实际上并未引用 footballClubs 的 name 属性。

所以你应该这样做:

func shuffle() {
let footballClubsInPoland = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: self.footballClubsInPoland) as! [footballClubs]
option1.setTitle(footballClubsInPoland[0].name, for: .normal)
option2.setTitle(footballClubsInPoland[1].name, for: .normal)
print(footballClubsInPoland[0])
print(footballClubsInPoland[1])

关于swift - 从随机排列的数组中设置按钮的标题 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46410398/

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