2017-07-20 30 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