What is the meaning of A. Type and A. self, where A is a class in Swift?

What is the meaning of A. Type and A. self, where A is a class in Swift?
June 14th 19 at 18:20
2 answers
June 14th 19 at 18:22
Solution
For any type T, T. the Type is a meta - (metatype), which is the type whose objects provide a description of the type T. Static functions and init-functions T become member functions of T. Type. Example:

struct Duck {
 static func kind() -> String { return "Bird" }
 init() { }
 func talk() { print("Quack") }
}

let meta: Duck.Type = Duck.self
meta.kind() //=> Bird
let obj: Duck = meta.init()
obj.talk() //=> Quack


There are 2 class metatype, but in practice, there are existential metatypes, i.e. their objects can be described as the type T, and any successor (subtype) of the type T. In other words, the set of objects of type T. the Type is U. self for all types U: T. Example:

class Animal { class var name: String { return "Animal" } }
class Cat: Animal { override class var name: String { return "Cat" } }
class Dog: Animal { override class var name: String { return "Dog" } }

var meta: Animal.Type
meta = Cat.self
meta.name //=> Cat
meta = Dog.self
meta.name //=> Dog


Or, for example, you can imagine such a function of the generation of animals:

class Animal { ... }

func createAnimal(_ type: Animal.Type) -> Animal {
 if type == Cat.self {
 return Cat(...)
 } else if type == Dog.self {
 return Dog(...)
 } else {
fatalError()
}
}


In practice, this functionality mitalipov is rarely used. As a rule, they are used for explicit specializations of template functions. For example:

unsafeCast func<t, u>(_: T, to: U. Type) -> U</t>

Here the only way to specify the type U when calling is to pass a "dummy" parameter of type U. the Typeas argument the type U feature is not takes. Rather, it is the best and adopted in a Swift method. You could get this:

unsafeCast func<t, u>(_: T, sameAs: U) -> U</t>

But, it is clear that you need to have at hand an object of type U, and it's not ideal. It is a pity that you can not simply write unsafeCast<u>(t)</u> when you call.

When using the meta - Type U. therefore, its value generally is ignored. That is, even if you give back the meta-heir to the U type, the function will still work with u

P. S. There are still many undocumented features mitalipov in Swift, but it's not very logical and thoughtful (even the developers of the Core team is not all there you know), so in practice it is better to restrict usage for explicit specializations of the functions as shown above.
Thank you! - alycia_Pfannerstill commented on June 14th 19 at 18:25
June 14th 19 at 18:24
Solution
Here is an example

class Person {
 var name: String
 required init(name: String) {
 self.name = name
}
}

func printPersonName(PersonType, Person.Type) {
 // PersonType, Person.Type
 let person = PersonType.init(name: Name)
print(person.name)
}

let PersonType = Person.self // Person.Type
printPersonName(PersonType: PersonType)


A. Type - used to specify the variable type in the function signature.
A. self - used directly in your code as a normal variable.
Rather than let PersonType = Person.self is different from let PersonType = Person(name: "Name")? - alycia_Pfannerstill commented on June 14th 19 at 18:27
In the first case, you define a variable PersonType type Person.Type ( a variable of class type), in the second case you are using this variable ( a variable of type class) to create a variable person of type Person - alycia_Pfannerstill commented on June 14th 19 at 18:30

Find more questions by tags iOSSwift