2017-07-20 31 views
1

我有这个数组,我创建: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

你需要解开它

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

这不起作用:我收到这种类型的值“串”说,没有成员“yourShareTitle” – josh

+0

更新我的回答 –

+0

感谢这么多的错误帮助! – josh