macspeed
macspeed

Reputation: 137

How to implement UICollectionView within ViewController.swift using Swift 3 for iOS

I am following along with Brian Voong for his tutorial: https://youtu.be/kecV6xPTTr8?list=PL0dzCUj1L5JHfozquTVhV4HRy-1A_aXlv

I am using the following code in ViewController.swift:

class ViewController: UIViewController {

let collectionView: UICollectionView {
let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    return cv
}()



override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(collectionView)
    collectionView.frame = view.frame


        }

I get two error messages which I don't understand as it is running for him:

  1. "'let' declarations cannot be computed properties" on 'let collectionView: UICollectionView {' line.
  2. '}()' provides another error message "Consecutive declarations on a line must be separated by ';'"

Would it be a function declaration or has swift been updated not to allow this syntax. Thank you in advance for any assistance.

Upvotes: 0

Views: 775

Answers (1)

noobular
noobular

Reputation: 3287

You're missing an equal sign:

let collectionView: UICollectionView = {

Example :

let collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    return cv
}()

Upvotes: 3

Related Questions