josh
josh

Reputation: 145

Accessing For in Loop Collection View Swift

I have this array that I created: let yourShares = ["90%","40%", "23%","15%","10%"] Then to acess this:

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

Then to access this I put this code in an extension for collection view

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

The problem is when I run this it just prints optional. Why, and how could I fix that? ~ Thanks

Upvotes: 2

Views: 57

Answers (1)

Vitaly Migunov
Vitaly Migunov

Reputation: 4457

You need to unwrap it

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

Upvotes: 2

Related Questions