Swift crashing when casting NSObject subclass to a non-objc protocol -


i've spent time figuring out. code crashes:

public protocol fourleggedanimal: anyobject {  }  public class animal: nsobject {  }  public class dog: animal, fourleggedanimal {  }   public class animalproperty<kind: animal>: nsobject {      let animal: kind      public init(animal: kind) {         self.animal = animal     }  }  public class fourleggedanimalproperty<kind: animal>: animalproperty<kind>, nstextfielddelegate {      public override init(animal: kind) {         /// since cannot express in swift kind should animal         /// subclass confirming particular protocol, use force-cast         /// not pretty solution, there aren't options.          /// , crashes.         let fourleggedanimal = animal as! fourleggedanimal         print(fourleggedanimal)          super.init(animal: animal)     }  }   let dog = dog() let property = fourleggedanimalproperty(animal: dog) 

the code crashes in swift's library's getgenericpattern() function when attempt cas dog fourleggedanimal protocol in intializer.

notes:

  • the correct way should declare generics <kind: animal, fourleggedanimal>, however, when so, can't call init(animal:) initializer dog, though it's animal , fourleggedanimal.
  • the app crashes when use soft-cast, i.e. it's not wrong cast, it's bug in swift's runtime.

the solution either declare fourleggedanimal @objc, or make animal not inherit nsobject.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -