Swift generic extension with self casting

Nik Kov

The goal is to dequeue cell like

 let cell = CustomCollectionViewCell.dequeueReusable(collectionView, for: indexPath)

I'm trying like

class func dequeueReusable<T: UICollectionViewCell>(_ collectionView: UICollectionView, for indexPath: IndexPath) -> T {
    return collectionView.dequeueReusableCell(withReuseIdentifier: self.reuseID, for: indexPath) as! T
}

But it returns UICollectionViewCell, not CustomCollectionViewCell.

How to achieve this?

Rob Napier

Your call to dequeueReusable never requires any particular type for T, so the most general is selected. The type you want, however, is Self (the type of the current subclass).

The natural (but slightly wrong) way to write this would be:

class func dequeueReusable(_ collectionView: UICollectionView, 
                           for indexPath: IndexPath) -> Self {
    return collectionView.dequeueReusableCell(withReuseIdentifier: self.reuseID,
                                                              for: indexPath) as! Self
}

I honestly don't know why this doesn't work. Self can't be used in the as! Self construct. It can, however, be tricked into working with a generic wrapper:

func cast<T>(_ value: Any) -> T { return value as! T }

With that, you get the working version:

class func dequeueReusable(_ collectionView: UICollectionView,
                           for indexPath: IndexPath) -> Self {
    return cast(collectionView.dequeueReusableCell(withReuseIdentifier: self.reuseID, 
                                                   for: indexPath))
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Type casting in a generic swift function

Casting a variable of generic data type to String in Swift

Swift: Casting a generic, FloatingPoint value to Int

String extension that modify self in swift

Swift Array extension with generic items

Extension of constructed generic type in Swift

Swift extension of generic type that is Optional

Generic parameter 'Self' could not be inferred (swift 4)

Generic parameter 'Self' could not be inferred swift

How to change self in NSDate extension Swift?

Swift protocol extension self reference issues with init

Swift protocol extension with specific Self type

Using 'self' in class extension functions in Swift

How can I make a generic extension in Swift?

Conforming a generic type to a protocol in a Swift extension

Extension on Array where element is a generic struct in Swift

How to make a constrained extension of Collection generic in Swift?

Pass in a type to a generic Swift extension, or ideally infer it

Generic Swift Dictionary Extension for nil filtering

Swift extension to MutableCollectionType not working with array of generic types

swift extension func with generic type - isEmpty for FetchedResults

How to restrict a Swift extension of a collection to a generic type?

Incorrectly Inferred Type in Generic Closure with Protocol Extension Constrained by Self

Generic-type extension with a new generic type in Swift

Swift extension - Constrained extension must be declared on the unspecialized generic type 'Array'

Casting in swift

Casting generic type object

Casting a generic variable java

Generic primitive type with casting