In Swift, what is the difference between two different customs?

In this example, Swift Xcode 6.0.1 for the table cell itself is used as a suffix (I don’t remember to use self-help before, as before) and the self prefix. (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) 
+6
source share
3 answers

Your first use of the self as a prefix is ​​a reference to an instance of a class that contains the method that is currently being called. In the second use, self refers to the type as a value, in this case UITableViewCell.self refers to the type UITableViewCell

+8
source

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

+3
source

I think this is because all over the world a type is used to input some variable. But SomeType.self says to use this type as a value. I don't think a type can stand on it unless you name .self .

Try the following on the 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) 
+2
source

Source: https://habr.com/ru/post/976032/


All Articles