Skip to content

Commit 66224ee

Browse files
committed
Parity: Bundle.classNamed(_:) and Bundle.principalClass.
.classNamed(_:) can now be used, though with a limitation noted in the comments. .principalClass is now implemented in terms of .classNamed(_:)
1 parent 7337331 commit 66224ee

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Foundation/Bundle.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,18 @@ open class Bundle: NSObject {
292292
}
293293
}
294294

295-
open func classNamed(_ className: String) -> AnyClass? { NSUnimplemented() }
296-
open var principalClass: AnyClass? { NSUnimplemented() }
295+
open func classNamed(_ className: String) -> AnyClass? {
296+
// FIXME: This will return a class that may not be associated with the receiver. https://bugs.swift.org/browse/SR-10347.
297+
guard isLoaded || load() else { return nil }
298+
return NSClassFromString(className)
299+
}
300+
301+
open var principalClass: AnyClass? {
302+
// NB: Cross-platform Swift doesn't have a notion of 'the first class in the ObjC segment' that ObjC platforms have. For swift-corelibs-foundation, if a bundle doesn't have a principal class named, the principal class is nil.
303+
guard let name = infoDictionary?["NSPrincipalClass"] as? String else { return nil }
304+
return classNamed(name)
305+
}
306+
297307
open var preferredLocalizations: [String] {
298308
return Bundle.preferredLocalizations(from: localizations)
299309
}

0 commit comments

Comments
 (0)