访问循环集合查看Swift

时间:2017-07-20 01:16:03

标签: swift

我创建了这个数组:let yourShares = ["90%","40%", "23%","15%","10%"]然后来访问:

 for yourShare in yourShares {
        dataSource[0].yourShareTitle = "90%"
        dataSource[1].yourShareTitle = "40%"
        dataSource[2].yourShareTitle = "23%"
        dataSource[3].yourShareTitle = "10%"
    }

然后要访问它,我将此代码放在集合视图的扩展名

let yourShare = model.dataSource?[indexPath.row] cell.yourShareLabel.text = "Your Share \(yourShare!.yourShareTitle)" cell.titleLabel?.font = UIFont.systemFont(ofSize: 1)

问题是当我运行它时它只打印可选。为什么,我怎么能解决这个问题? 〜谢谢

1 个答案:

答案 0 :(得分:1)

你需要打开它

if let yourShare = model.dataSource?[indexPath.row]?.yourShareTitle as? String {
        cell.yourShareLabel.text = "Your Share \(yourShare)"
        cell.titleLabel?.font = UIFont.systemFont(ofSize: 1)
}