Reputation: 235
apple has some sample code called adventure found here:https://developer.apple.com/library/ios/samplecode/Adventure-Swift/Introduction/Intro.html
in the function inferCharacterType they use Character.Type to determine the which subclass of character is used. what is .Type . I option click it and it is not defined and without documentation. The classes passed to inferCharacterType are inherit from the character class. Does .Type indicate the identity of a specific type of character(one of the classes that inherits from character)
// This function uses pattern matching to infer the appropriate enum value based on the type provided.
func inferCharacterType(fromType: Character.Type) -> CharacterType {
switch fromType {
case is Goblin.Type:
return CharacterType.Goblin
case is Cave.Type:
return CharacterType.Cave
case is Boss.Type:
return CharacterType.Boss
case is Warrior.Type:
return CharacterType.Warrior
case is Archer.Type:
return CharacterType.Archer
default:
fatalError("Unknown type provided for \(__FUNCTION__).")
}
}
so i did some experimentation and intentionally cause an error to determine the identity of self. I found that only "class func" shows myClass.Type as self and "func" shows myClass
here are the pictures
the next picture
I would like a reference to where this information can be looked up. Thanks Any help is appreciated
Upvotes: 0
Views: 155
Reputation: 37043
Methods preceded with class
or static
are "type methods". You can find out more from the Swift Programming Language book, in the section on type methods.
Type methods are methods that are called on a type itself, rather than on an instance of that type.
let object = MyClass()
object.doSomething() // instance method
MyClass.doSomethingElse() // type method
As such, instead of self
refering to the receiving instance (as with instance methods), it refers to the type itself when used inside a type method.
.Type
itself refers to the metatype type, that is to say, the type of the type.
Upvotes: 1