Reputation: 33
Tried this and it gives me an error:
class BaseClass {
class var testProperty: String {
return "Original"
}
class func testingFunc()->Self {
return self // error - Can not convert return expression of type "Self.Type" to return the type "Self"
}
}
Any thought? Thanks
Upvotes: 3
Views: 1990
Reputation: 3541
In a class/static function, self
refers to the Class Type. There is no instance to refer to, so what you get is the type, which is the current scope. It's not the same in an instance method, where self refers to <instance>.self
class Foo {
class func classMethod() -> Foo.Type {
return self // means Foo.self
}
func instanceMethod() -> Foo {
return self // means <instance of Foo>.self
}
}
Upvotes: 8
Reputation: 80265
If you want to get info about an instance, use an instance function. Omit the keyword class
when defining the function.
class BaseClass {
func instanceInfo() {
// self here refers to the instance
}
}
Upvotes: -2
Reputation: 589
Your return type should be the class type you are in. In this case it isBaseClass
.
Also, you are defining a class function which is basically a static function. It will not return an instance. I don't really understand what you are trying to accomplish.
Upvotes: 0