声明没有类型的变量

时间:2015-12-22 13:57:50

标签: ios swift

我需要声明一个变量,如下所示:

var cell

if cond {
    cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? CustomCell1
}
else {
    cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? CustomCell2
}

这里的问题是我收到错误Type annotation missing in pattern

我应该声明我的变量是什么类型,或者是否有解决方法?

1 个答案:

答案 0 :(得分:4)

假设<div class="media"> <span class="media-left"> <img class="media-object" src="assets/images/cds/cd1.png" alt="CD cover for first CD."> </span> <div class="media-body"> <h2 class="media-heading">Name of CD</h2> <ol> <li>Song<span>Composer</span></li> <li>Song<span>Composer</span></li> <li>Song<span>Composer</span></li> <li>Song<span>Composer</span></li> <li>Song<span>Composer</span></li> <li>Song<span>Composer</span></li> </ol> </div> </div> <div class="media"> <span class="media-left"> <img class="media-object" src="assets/images/cds/cd2.png" alt="CD cover for second CD."> </span> <div class="media-body"> <h2 class="media-heading">Name of CD</h2> <ol> <li>Song<span>Composer</span></li> <li>Song<span>Composer</span></li> <li>Song<span>Composer</span></li> <li>Song<span>Composer</span></li> <li>Song<span>Composer</span></li> <li>Song<span>Composer</span></li> <li>Song<span>Composer</span></li> <li>Song<span>Composer</span></li> </ol> </div> </div> CustomCell1继承自CustomCell2,您可以执行以下操作:

UICollectionViewCell

然后,当您想将其用作特定类型的单元格时,请使用:

var x: UICollectionViewCell?

if cond {
    x = CustomCell1()
}
else {
    x = CustomCell2()
}