Hi,
I noticed some unexpected behavior when I tried to subclass from Bundle.
//: Playground - noun: a place where people can play
import Foundation
class Test : Bundle {
var foo = "Hey"
override init?(path: String) {
super.init(path: path)
}
}
let mainB = Test(path: Bundle.main.bundlePath)
print(mainB?.foo ?? "Fallback")Will crash when I try to access a property of the subclass (line 12) with:
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=2, address=0x...).
This crash only occurs if I use the main bundlePath. To me this looks like an error within Foundation/Swift. If it is invalid/forbidden to create a second instance of the main bundle the failable init should fail and return nil. But to receive an object that isn't properly initialized/allocated is just wrong.
Is this a Bug within the Foundation Framework or do I understand something wrong?
Best Regards
Timo
The actual behavior is documented (https://developer.apple.com/documentation/foundation/bundle/1412741-init):
"This method initializes and returns a new instance only if there is no existing bundle associated with fullPath, otherwise it deallocates self and returns the existing object."
It's also documented in the NSBundle.h header file:
/* Because NSBundle caches allocated instances, subclasses should be prepared
to receive an already initialized object back from [super initWithPath:] */