Here's a recreate example with swift repl:
import Foundation
protocol DynamicCounter {
init(_ isPlayer: Bool)
}
class BaseCounter: NSObject, DynamicCounter {
required init(_ isPlayer: Bool) {
}
}
class TestCounter: BaseCounter {
required init(_ isPlayer: Bool) {
super.init(isPlayer)
}
}
class Helper {
static func withAllClasses<R>(
_ body: (UnsafeBufferPointer<AnyClass>) throws -> R
) rethrows -> R {
var count: UInt32 = 0
let classListPtr = objc_copyClassList(&count)
defer {
free(UnsafeMutableRawPointer(classListPtr))
}
let classListBuffer = UnsafeBufferPointer(
start: classListPtr, count: Int(count)
)
return try body(classListBuffer)
}
static func initialize() {
let monoClasses = withAllClasses { $0.compactMap { $0 as? BaseCounter.Type } }
for cl in monoClasses {
cl.init(false)
}
}
}
Helper.initialize()
output:
2024-10-29 15:30:48.760664-0400 repl_swift[13210:34621315] *** NSForwarding: warning: object 0x200b5cc48 of class '__NSGenericDeallocHandler' does not implement methodSignatureForSelector: -- trouble ahead
2024-10-29 15:30:48.761418-0400 repl_swift[13210:34621315] *** NSForwarding: warning: object 0x200b5cc48 of class '__NSGenericDeallocHandler' does not implement doesNotRecognizeSelector: -- abort
Execution interrupted. Enter code to recover and continue.
Topic:
Programming Languages
SubTopic:
Swift
Tags: