Is it possible to make a category (extension) that would eventually return an object casted to instancetype
? I have a category to loads SKS files but since this category is for SKNode
then all other subclasses like SKScene
or SKEmitterNode
, etc.. will adopt it too.
So I would simply like to avoid always casting from SKNode
to instancetype
. Is it possible to change return type to instancetype
and make sure that compiler is happy with return value?
I think I can use -> Self
as return type but then I have no idea how to cast scene
to instancetype so this thing would compile..
For example:
SKEmitterNode.unarchiveFromFile("Blah")
would return an instance of SKEmitterNode
extension SKNode {
class func unarchiveFromFile(file: String) -> SKNode {
let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks")
var sceneData = NSData.dataWithContentsOfFile(path, options: .DataReadingMappedIfSafe, error: nil)
let unarchiver = NSKeyedUnarchiver(forReadingWithData: sceneData)
unarchiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
let scene = unarchiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as SKNode
unarchiver.finishDecoding()
return scene
}
}