title: 'UICollectionView must be initialized with a non-nil layout parameter' - Swift 4
body: <p>(Don't mark this question as duplicated. I read a lot of questions but I don't find the answer to my issue.)</p>
  
  <p>I have created a <code>IUCollectionView</code> class and a <code>UICollectionViewCell</code> class. Here is the collection view one: </p>
  
  <pre><code>class NearbyPlacesUICollectionView: UICollectionView {
  
  
      required init(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)!
          commonInit()
          fatalError("init(coder:) has not been implemented")
      }
  
      override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
          super.init(frame: frame, collectionViewLayout: layout)
          commonInit()
      }
  
      init(collectionView: UICollectionView, frame: CGRect) {
          let layout = UICollectionViewLayout()
          super.init(frame: frame, collectionViewLayout: layout)
      }
  
      func commonInit() {
  
          allowsMultipleSelection = false
          backgroundColor = .white
  
          let layout = UICollectionViewFlowLayout()
          layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
          layout.itemSize = CGSize(width: UIScreen.main.bounds.width * 0.8875, height: 90*3)
          frame = UIScreen.main.bounds
          collectionViewLayout = layout
  
          register(NearbyPlacesUICollectionView.self, forCellWithReuseIdentifier: "nearbyPlaceCell")
      }
  }
  </code></pre>
  
  <p>Then I have a <code>UIViewController</code> where I have this (I remove some code to make it shorter): </p>
  
  <pre><code>    class NearbyPlacesViewController: UIViewController, UICollectionViewDataSource {
  
         var collectionView = NearbyPlacesUICollectionView()
  
         override func viewDidLoad() {
              super.viewDidLoad()
  
          collectionView.dataSource = self
          collectionView.delegate = self
          view.addSubview(collectionView)
         }
  }
  
  extension NearbyPlacesViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
  
      func numberOfSections(in tableView: UITableView) -&gt; Int {
          return 3
      }
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return 2
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
          let cell: NearbyPlacesUICollectionViewCell? = collectionView.dequeueReusableCell(withReuseIdentifier: "nearbyPlaceCell", for: indexPath) as? NearbyPlacesUICollectionViewCell
  
          return cell!
      }
  
      func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
          if let cell = cell as? NearbyPlacesUICollectionViewCell
          {
              _configureCell(cell, atIndexPath: indexPath)
          }
      }
  
      fileprivate func _configureCell(_ cell: NearbyPlacesUICollectionViewCell, atIndexPath indexPath: IndexPath)
      {
          cell.queue.cancelAllOperations()
  
          let operation: BlockOperation = BlockOperation()
          operation.addExecutionBlock { [weak operation] () -&gt; Void in
  
              DispatchQueue.main.sync(execute: { [weak operation] () -&gt; Void in
  
                  if let operation = operation, operation.isCancelled { 
              return }
  
                  ... // cell code here
              })
          }
  
          cell.queue.addOperation(operation)
      }
  
  
      func collectionView(_ collectionView: UICollectionView, viewForHeaderInSection section: Int) -&gt; UIView? {
  
          let view = UIView()
          view.backgroundColor = .clear
              ... // header code here
  
          return view
      }
  }
  </code></pre>
  
  <p>I get this error when running: <code>UICollectionView must be initialized with a non-nil layout parameter</code>
  I override all the init but it seems like they are not called. I read all questions about similar issues but I didn't find any answer. (I'm using Swift 4). Sorry If I paste a lot of code but I think it's necessary. Thanks in advance for your help.</p>