Given this function:
func createEmptyArrayWithViewType(viewType: UIView.Type) -> [UIView] {
/// Create array of viewType views
}
How do I create an empty array with that function? viewType parameter can be any UIView subclass (UIImageView, UILabel, etc.) Is it possible without using generics?
UPDATE
Ok I think some more context would help.
What I am trying to build is a reuse queue that pretty much does what UITableView and UICollectionView provide.
This is my class's current code:
class SwipeView: UIView {
private var _reuseQueues = [String: [MatchSwipeCell]]()
...
}
/// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//MARK: - Reuse queue
/// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
extension SwipeView {
func registerClass(cellClass: SwipeCell.Type, forCellWithReuseIdentifier identifier: String) {
if let reuseQueue = _reuseQueues[identifier] {
fatalError("Reuse identifier '\(identifier)' was already registered with class '\(reuseQueue.dynamicType.Element.self)'")
}
/// This is not working!
_reuseQueues[identifier] = [cellClass]()
}
func dequeueReusableCellWithReuseIdentifier(identifier: String) -> SwipeCell {
guard var reuseQueue = _reuseQueues[identifier] else {
fatalError("No class was registered for the reuse identifier '\(identifier)'")
}
if let cell = reuseQueue.first {
reuseQueue.removeFirst()
cell.prepareForReuse()
return cell
}
return reuseQueue.dynamicType.Element(reuseIdentifier: identifier)
}
private func enqueueCellForReuse(cell: SwipeCell) {
_reuseQueues[cell.reuseIdentifier]?.append(cell)
}
}
return Array<UIView>()Since the return isn't generic it doesn't matter what subclass you enter as a param. There is no way/need/reason to specialise it in any way, since the return would undo all that.