title: 'UICollectionView must be initialized with a non-nil layout parameter' problem arises even when layout parameter is set
body: <p>You can see from the code below that layout parameter has been set inside <code>ViewDidLoad()</code>, but it gives me an error:</p>
  
  <blockquote>
    <p>Terminating app due to uncaught exception
    'NSInvalidArgumentException', reason: 'UICollectionView must be
    initialized with a non-nil layout parameter'</p>
  </blockquote>
  
  <pre><code>var layer: UICollectionViewFlowLayout = {
  
          var item = UICollectionViewFlowLayout()
  
          item.itemSize = CGSize(width: 100, height: 100)
  
          return item
  
      }()
  </code></pre>
  
  <p><code>UICollectionView</code> object has been set in property initializer because obviously I want this <code>UICollectionView</code> object to be accessible globally:</p>
  
  <pre><code>var dataCollectionViews = UICollectionView()
  
  viewDidLoad(){
  
  dataCollectionViews = UICollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0) , collectionViewLayout: self.layer)
  
          view.addSubview(dataCollectionViews)
  
          self.dataCollectionViews.delegate = self
          self.dataCollectionViews.dataSource = self
          self.dataCollectionViews.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewReuse)
          self.dataCollectionViews.layer.cornerRadius = 10
          self.dataCollectionViews.translatesAutoresizingMaskIntoConstraints = false
          self.dataCollectionViews.isHidden = true
  
    changed()
  }
  </code></pre>
  
  <p>When I moved contents inside <code>changed()</code> to inside of <code>viewDidLoad()</code> and initialized dataCollectionViews inside of <code>viewDidLoad()</code>, everything worked as intended, showing <code>UICollectionView</code> object when selecting first button of segmented Control. </p>
  
  <pre><code>@objc func changed() {
  
      if segmentTime.selectedSegmentIndex == 0 {
  
          self.dataCollectionViews.isHidden = false
  
          let dataCollectionTop = NSLayoutConstraint(item: self.dataCollectionViews, attribute: .top, relatedBy: .equal, toItem: sortLabel, attribute: .bottom, multiplier: 1.0, constant: 10)
  
          let dataCollectionLeft = NSLayoutConstraint(item: self.dataCollectionViews, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 10)
  
          let dataCollectionRight = NSLayoutConstraint(item: self.dataCollectionViews, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1.0, constant: -10)
  
          let dataCollectionHeight = NSLayoutConstraint(item: self.dataCollectionViews, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 520/812, constant: 0)
  
          let dataCollectionWidth = NSLayoutConstraint(item: self.dataCollectionViews, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 355/375, constant: 0)
  
          NSLayoutConstraint.activate([dataCollectionLeft, dataCollectionRight, dataCollectionHeight, dataCollectionWidth, dataCollectionTop])
  
      }
  
      if segmentTime.selectedSegmentIndex == 1 {
  
  
  
      }
  
      if segmentTime.selectedSegmentIndex == 2 {
  
  
  
      }
  
  }
  </code></pre>
  
  <p>I'd love to try to set dataCollectionViews object in property initializer but I know it is not possible, but it seems like <code>dataCollectionViews</code> needs be initialized with <code>UICollectionViewFlowLayout()</code> on first try. What other alternative is available?</p>