Reputation: 3477
I need to declare a variable as follows:
var cell
if cond {
cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? CustomCell1
}
else {
cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? CustomCell2
}
The problem here is that I get the error Type annotation missing in pattern
.
Of what type should I declare my variable or is there a workaround this issue?
Upvotes: 1
Views: 880
Reputation: 7373
Assuming that CustomCell1
and CustomCell2
inherit from UICollectionViewCell
you can do the following:
var x: UICollectionViewCell?
if cond {
x = CustomCell1()
}
else {
x = CustomCell2()
}
Then when you want to use it as a specific type of cell you use this:
if let cell1 = x as? CustomCell1 {
//Use cell1 here
}
if let cell2 = x as? CustomCell2 {
//Use cell2 here
}
Upvotes: 5