swift - Casting a class to subclass fails -


edit - sorry guys, don't think gave enough detail:

in parse's ios framework, class called pfobject (classa). framework allows subclass (classb) pfobject. and, if scenario such 1 gave should occur, no issue.

my question how possible? understand subclass contain variables/methods not present in main class, making process of casting class subclass unreasonable/unsafe.

they have protocol defined pfsubclassing, don't see how allows behaviour occur.


i've got following in playground:

class classa: nsobject { } class classb: classa { }  let objecta = classa() let objectb = objecta as! classb // <- crashes 

questions:

  • why crash occurring?
  • is there way around this?

you know how inheritance works right? class b inherits class a. when make instance of classa , try cast class b, many different things might happen because undefined behavior. problem here what's called slicing. compiler refuses cast down parent class child class because knows child class may have different info (variables, func. etc) , doesn't know how translate instance of super classes child class. however, going other way around fine, i.e

let objectb = classb() let objecta = objectb as? classa 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -