Reputation: 15245
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
Upvotes: 6
Views: 8029
Reputation: 25619
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")
}
}
Upvotes: 3
Reputation: 49
Setting let 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 the let variables with self.
Hope that it works for you.
Upvotes: 1
Reputation: 25459
At that point there is not collectionFlowLayout
created so it complains that there is no member named like that.
The solution can be as you mentioned to make it optional and initialise it in init or you can do this:
let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
Upvotes: 0