Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to demangle superclass with Cocoapods in Xcode 10.2

After moving to Xcode 10.2, when running my app I get a crash with the error failed to demangle superclass of MyClass from mangled name MySuperClass.

The crash occurs when I try to create an instance of MyClass. I am using CocoaPods 1.6.1 and have not yet upgraded to Swift 5. The class in question is defined inside a Pod, and is a subclass of a class defined a different Pod (listed as a sub dependency of the first Pod).

Adding to the complexity (unsure if it is related) is that the super class takes a generic, and the sub class defines a concrete type and does not take a generic. I.e.

// Inside Pod B:
open class MySuperClass<DataType: Decodable> { ... }

// Inside Pod A:
open class MySubClass: MySuperClass<AConcreteStructConformingToCodable> { ... }

// Inside my project:
let myClass = MySubClass()

I have tried overriding the Pod build settings to build with and without optmisation without any change in behaviour.

like image 307
JoGoFo Avatar asked Apr 09 '19 02:04

JoGoFo


1 Answers

I have discovered this in the Xcode 10.2 Release Notes, under "Swift Compiler" -> "Known Issues":

Linking against a static Swift library might create a binary with missing type metadata because the object files that define the metadata inside the static archive are mistakenly considered unused. (47598583)

This can manifest as a Swift runtime error with a message such as: “failed to demangle superclass of MyClass from mangled name ‘’”.

Workaround: If you can rebuild the static library, try building it with whole module optimization enabled. Otherwise, add -all_load to the linker flags in the client binary to ensure all object files are linked into it.

And I was able to resolve the issue by adding -all_load to Other Linker Flags of the main Project:

-all_load linker flag

Hope this helps someone else!

like image 67
JoGoFo Avatar answered Sep 19 '22 17:09

JoGoFo