0

Just wondering why I can't access the inherited object collectionView when lazy initializing:

class FunCollectionLayout : UICollectionViewFlowLayout {
    var middleSection:Int = {
        let sectionCount = self.collectionView!.numberOfSections()
        return sectionCount/2
    }()

    func testFunc() {
        print((self.collectionView?.numberOfSections())! / 2)
    }
}

The error is:

Value of type 'NSObject -> () -> FunCollectionLayout' has no member 'collectionView'

1 Answer 1

1

You are simply missing the lazy declaration attribute.

  lazy var middleSection:Int = {
    let sectionCount = self.collectionView!.numberOfSections()
    return sectionCount/2
  }()

But you are missing the point by not making this a computed property.

  var middleSection: Int {
    let sectionCount = self.collectionView!.numberOfSections()
    return sectionCount / 2
  }

Keep it dynamic, keep it in sync with the collectionView, make it a computed property.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.