Problem with accessabilityLabel demo p.442

I am unable to make this work. When I checked the Chapter 24 sample code (download) this code is omitted :confused:

Code from PhotoCollectionViewCell

class PhotoCollectionViewCell: UICollectionViewCell {

var photoDescription:String?

override var accessibilityLabel: String? {
    get {
        return photoDescription
    }
    set {
        // Ignore
    }
}

Code from PhotoDataSource:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let identifier = "PhotoCollectionViewCell"
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
    
    let photo = photos[indexPath.row]
    cell.photoDescription = photo.title // <<< Compiler Error Here !!!
    
    return cell
}

Compiler Error: “‘UICollectionViewCell’ has no member ‘photoDescription’”

Any Ideas?

found it!! (Dolt!)

The line “let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)” was missing “as! PhotoCollectionViewCell” at the end.

1 Like

Thank you past toms for your studious corrections! :kissing_heart:

(I totally see the missing code in my book now looking back with a more watchful eye, while it might have been an update… I probably just fat fingered the code when I was hand copying it over originally)

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let identifier = "PhotoCollectionViewCell" 
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! PhotoCollectionViewCell

let photo = photos[indexPath.row] cell.photoDescription = photo.title

return cell

}