Reputation: 3820
In this Swift Xcode 6.0.1 example for a table cell, .self is used as a suffix (don't remember seeing self used like that before), and a prefix self. (which of course is everywhere), trying to understand what that really means.
// Register the UITableViewCell class with the tableView
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.cellIdentifier)
Upvotes: 6
Views: 867
Reputation: 187014
I think it's because everywhere else the type is used to type some variable. But SomeType.self
says to use this type as the value. I don't think a type can stand on it's own, unless you call .self
.
Try the following in a playground.
class Foo {}
Foo // ^ Compiler error: expected member name or constructor call after type name
But with .self
class Foo {}
Foo.self // console reports: (Metatype)
Upvotes: 3
Reputation: 70122
Your first usage of self as a prefix is a reference to the instance of the class that contains the method that is currently being invoked. In the second usage, self is referring to a type as a value, in this case UITableViewCell.self
refers to the UITableViewCell
type
Upvotes: 8
Reputation: 410622
self
is a method that returns the receiver of the message. In this case, it returns the Class
object for UITableViewCell
.
Upvotes: 3