Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass in Type as Parameters #8

Closed
k3zi opened this issue Feb 4, 2016 · 3 comments · Fixed by #11
Closed

Pass in Type as Parameters #8

k3zi opened this issue Feb 4, 2016 · 3 comments · Fixed by #11

Comments

@k3zi
Copy link
Contributor

k3zi commented Feb 4, 2016

I ran across an issue where Swift wasn't allowing me to supply a type that was produced from a function

func classTypeForTable(tableView: UITableView) -> ReusableCell.Type {
     return MyCell.Type
}

// In cellForRow //
let cellClass = classTypeForTable(tableView)
let cell = tableView.dequeueReusableCell(indexPath) as cellClass

^^^^^^ This last line fails because "cellClass is not a type"

I was able to get around this by changing the dequeue function and adding another:

func dequeueReusableCell<T: UITableViewCell where T: Reusable>(indexPath indexPath: NSIndexPath) -> T {
        return self.dequeueReusableCell(indexPath, cellType: T.self)
    }

func dequeueReusableCell<T: UITableViewCell where T: Reusable>(indexPath: NSIndexPath, cellType: T.Type) -> T {
       return self.dequeueReusableCellWithIdentifier(T.reuseIdentifier, forIndexPath: indexPath) as! T
    }

This would seem like a function you might want to add for advanced users...

@AliSoftware
Copy link
Owner

Could be a great addition in case you have the type in a variable indeed 👍

BTW, wouldn't it be even simpler to implement it using a default value for this additional parameter?

func dequeueReusableCell<T: UITableViewCell where T: Reusable>(indexPath: NSIndexPath, cellType: T.Type = T.self) -> T {
  return self.dequeueReusableCellWithIdentifier(T.reuseIdentifier, forIndexPath: indexPath) as! T
}

This way we'd get both implementations for free in less code 😉


Don't hesitate to make a Pull Request to add this feature.

I definitely need to implement #7 soon so people can contribute with the assurance they won't break anything, but in the meantime if you use the Sample project to demonstrate that alternate use case, so it could at least confirm things can continue to work both with the current syntax and with your new API using the additional parameter.

@k3zi
Copy link
Contributor Author

k3zi commented Feb 5, 2016

Like the merged function! Didn't realize I could put them together...

I'll go ahead and create a PR when I can.

k3zi added a commit to k3zi/Reusable that referenced this issue Feb 13, 2016
@k3zi
Copy link
Contributor Author

k3zi commented Feb 13, 2016

It was also necessary for me to replace T.reuseIdentifier with cellType.reuseIdentifier because T is a reference to the superclass of cellType (in the above example: ReusableCell).

k3zi added a commit to k3zi/Reusable that referenced this issue Feb 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants