2

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)
  }
}
5
  • I think that using a switch statement on the view type in your function is the only way to do it. Commented Jan 2, 2016 at 17:01
  • 3
    Is there any reason you are trying to build something generic without generics? I do not see the use case for what you are asking for! Commented Jan 2, 2016 at 17:09
  • it's very simple. Just use 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. Commented Jan 2, 2016 at 17:15
  • Seen your update, why no generics??? Commented Jan 2, 2016 at 17:43
  • in a UITableView it also just uses UITableViewCell instead of specific subclasses. You still have to downcast them. Just use the same method. Commented Jan 2, 2016 at 18:17

1 Answer 1

1

Wait, you merely want to create an empty array of UIViews? Your function would then look like this:

func createEmptyArrayWithViewType(viewType: UIView.Type) -> [UIView] {
    return []
}

viewType parameter is not needed. You can put into this array any view that you want (UIImageView, UILabel, etc.)

var array = createEmptyArrayWithViewType(UIImageView)
array.append(UIImageView())
array.append(UILabel())

If you want to have you function return [UIView] you just end up with empty array of UIViews

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.