swiftlang / swift-corelibs-foundation

The Foundation Project, providing core utilities, internationalization, and OS independence
swift.org
Apache License 2.0
5.3k stars 1.14k forks source link

[SR-953] Please implement NSBundle.init(forClass:) #4575

Open swift-ci opened 8 years ago

swift-ci commented 8 years ago
Previous ID SR-953
Radar None
Original Reporter vadim (JIRA User)
Type Task
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 2 | |Component/s | Foundation | |Labels | Task | |Assignee | mamabusi (JIRA) | |Priority | Medium | md5: 5b239564779b3473db72152901b916df

Issue Description:

https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSBundle.swift:
public init(forClass aClass: AnyClass) { NSUnimplemented() }

swift-ci commented 8 years ago

Comment by Yan Li (JIRA)

I looked into this one today.

First I thought it can be easily implemented by enumerating all loaded bundles and try to find where the class lives. It can be something like this:

let allBundles = NSBundle.allFrameworks() + NSBundle.allBundles()

let bundlesContainingClass = allBundles.filter {
    return $0.loaded
}.filter {
    return $0.classNamed(aClass.description()) != nil
}

return bundlesContainingClass.first!

But then I found out that many methods this implementation depends on are actually based on Objective-C runtime, for example: NSBundle.load(), NSBundle.classNamed(: ), etc. And in corelibs they are also NSUnumplemented().

So I think this task is actually depending on some other deeper tasks.