Reputation: 766
I'm kind of stuck trying to understand .self
in swift, always I register a custom cell in an collectionView or an tableView I have to register like customCollectionViewCell.self
but why not for example customCollectionViewCell()
?
I've tried in the playground to type String.self
in a var to see what happens and it give me no errors but when I try to give it a value it give me the following error
Cannot assign value of type 'String' to type
String.Type
what does it mean?
Upvotes: 0
Views: 640
Reputation: 54706
Type()
is shorthand notation for Type.init()
, so String()
calls String.init()
and hence initialises a String
.
On the other hand, String.self
returns the meta type of String
, which is a type that represents the String
type itself rather than any instance of that type. For more information on meta types, see the Metatype Type section of the official Swift language reference.
In your specific example, you need to register a UITableViewCell
subclass type rather than an instance of a specific type, hence you need to use YourCellType.self
.
Upvotes: 5