从混洗数组设置按钮的标题 - Swift

时间:2017-09-25 16:57:02

标签: swift swift4

这是我的代码

    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])
}

我的问题是我可以在控制台中看到被洗牌的对象,但我的按钮的标题没有在模拟器中加载。我该怎么办?

1 个答案:

答案 0 :(得分:0)

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

所以你应该这样做:

footballClubs