Skip to content

Parity: Bundle.classNamed(_:) and Bundle.principalClass. #2073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Foundation/Bundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,18 @@ open class Bundle: NSObject {
}
}

open func classNamed(_ className: String) -> AnyClass? { NSUnimplemented() }
open var principalClass: AnyClass? { NSUnimplemented() }
open func classNamed(_ className: String) -> AnyClass? {
// FIXME: This will return a class that may not be associated with the receiver. https://bugs.swift.org/browse/SR-10347.
guard isLoaded || load() else { return nil }
return NSClassFromString(className)
}

open var principalClass: AnyClass? {
// 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.
guard let name = infoDictionary?["NSPrincipalClass"] as? String else { return nil }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the principal class is not specified in the information dictionary, the bundle identifies the first class loaded as the principal class. When several classes are linked into a dynamically loadable file, the default principal class is the first one listed on the ld command line.

Reading the documentation looks like the principle-class-not-found case is not handle here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the case the comment notes, and the comment notes why we don't handle it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for confirming.

return classNamed(name)
}

open var preferredLocalizations: [String] {
return Bundle.preferredLocalizations(from: localizations)
}
Expand Down