Swift 'has no member named'

Is there a solution to this problem?

class ViewController : UIViewController { let collectionFlowLayout = UICollectionViewFlowLayout() let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: collectionFlowLayout) } 

xcode gives me the following error

 ViewController.swift: 'ViewController.Type' does not have a member named 'collectionFlowLayout' 

I could make it optional and initialize it in the init method, but I'm looking for a way to make a collection of view let, not var

+6
source share
3 answers

You can assign seed values ​​to constant member variables in your initializer. There is no need to make it var or optional.

 class ViewController : UIViewController { let collectionFlowLayout = UICollectionViewFlowLayout() let collectionView : UICollectionView override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { self.collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: self.collectionFlowLayout); super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil); } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } 
+3
source

Setting variables (constants) in the init method:

 class ViewController : UIViewController { let collectionFlowLayout: UICollectionViewFlowLayout! let collectionView: UICollectionView! init() { super.init() self.collectionFlowLayout = UICollectionViewFlowLayout() self.collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: collectionFlowLayout) } } 

We can access let variables with self.

Hope this works for you.

+1
source

At this point, collectionFlowLayout is not created, so it complains that no such name exists.

The solution may be what you said to make it optional and initialize it in init, or you can do this:

 let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout()) 
0
source

Source: https://habr.com/ru/post/977821/


All Articles