Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 'does not have a member named'

Is there a solution for 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 an optional and initialise it in the init method, but i'm looking for a way to make the collectionview a let and not a var

like image 856
Andy Jacobs Avatar asked Nov 06 '14 09:11

Andy Jacobs


1 Answers

You can assign initial values to constant member variables in your initializer. There's no need to make it a 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")
    }
}
like image 110
Darren Avatar answered Sep 20 '22 01:09

Darren