那是我的代码
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
时,实际上并没有引用name
的footballClubs
属性。
因此,您应该这样做:
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/