Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Swift, what is the difference between the two different usages of self?

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)
like image 491
Edward Potter Avatar asked Sep 29 '14 21:09

Edward Potter


2 Answers

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

like image 135
ColinE Avatar answered Oct 02 '22 14:10

ColinE


self is a method that returns the receiver of the message. In this case, it returns the Class object for UITableViewCell.

like image 28
mipadi Avatar answered Oct 02 '22 14:10

mipadi