Getting the name of a class in Swift is pretty easy like so:
import Foundation
class Gen<T> {
init() { }
}
func printName(obj: AnyObject) {
dump(NSStringFromClass(obj.dynamicType))
}
let a: Gen<String> = Gen()
let b: Gen<Int> = Gen()
printName(a)
printName(b)
However if you run the above code twice, it will not produce the same result.
So is there a way to implement printName
(get at a specialized generic class's name) in a stable way? That is multiple runs of the code prints the same string for the same class?
I'm using the latest Xcode beta.
Some extra requirements:
printName
is high.Sorry to disappoint, but this does not seem to be possible, unless you actively write a new compiler.
Check this source out. The author describes it quite well: Note that the isa pointer of the two objects is not identical, even though both are an instance of WrapperClass. Evidently, a specialization of a generic class is a separate class at runtime.
What I was also playing around with was @objc
, but this is just changing the namespace of the class, but obviously has no influence on the class name.
New starting from Xcode 6.3 beta 1
Good news! Starting from 6.3 you can do:
toString(Gen<Int>())
And that'd print: Gen<Swift.Int>
OLD Answer:
If the scope can be reduced to T: AnyObject then you can implement it as follows:
class Gen<T: AnyObject> {
var className: String {
return "Gen<\(NSStringFromClass(T.self))>"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With