Is it possible for Xcode/linker to automatically link -ObjC when linking static libraries that contain Swift or ObjC code?
Without that it is possible for a protocol conformance in a static library to be seemingly stripped from the library. For example, if you have the following protocol hierarchy in a static library:
protocol P {}
protocol Q: P {}
And you create a conformance to P
and Q
in separate files:
// ConformanceP.swift
struct Conformance: P {}
// ConformanceQ.swift
extension Conformance: Q {}
Then, in a separate library, checking for a dynamic conformance fails, but a direct check succeeds:
func isAnyQ(_ p: any P) -> Bool {
p is any Q
}
func checkConformanceIndirectly() -> Bool {
isAnyQ(Conformance())
}
checkConformanceIndirectly() // false
Conformance() is any Q // true
Once can fix this by adding -ObjC to the linker flags of the application.
I previously thought this was a bug and filed a feedback (see: FB11779019), but then was told this is the expected behavior (see here).
We have had a few people run into this problem in one of our open source libraries. See here for an example.