Skip to content

Commit a1d15a1

Browse files
committed
Merge pull request #275 from seabaylea/nsbundle
SR-794 pass supplied subpath through to URLForResource rather than nil
2 parents d12c9b7 + 55e9523 commit a1d15a1

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Foundation/NSBundle.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public class NSBundle : NSObject {
205205
}
206206

207207
public func pathForResource(name: String?, ofType ext: String?, inDirectory subpath: String?) -> String? {
208-
return self.URLForResource(name, withExtension: ext, subdirectory: nil)?.path
208+
return self.URLForResource(name, withExtension: ext, subdirectory: subpath)?.path
209209
}
210210

211211
public func pathForResource(name: String?, ofType ext: String?, inDirectory subpath: String?, forLocalization localizationName: String?) -> String? {

TestFoundation/TestNSBundle.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ class TestNSBundle : XCTestCase {
9898

9999
private let _bundleName = "MyBundle.bundle"
100100
private let _bundleResourceNames = ["hello.world", "goodbye.world", "swift.org"]
101+
private let _subDirectory = "Sources"
102+
private let _main = "main"
103+
private let _type = "swift"
101104

102105
private func _setupPlayground() -> String? {
103106
// Make sure the directory is uniquely named
@@ -114,6 +117,10 @@ class TestNSBundle : XCTestCase {
114117
for n in _bundleResourceNames {
115118
NSFileManager.defaultManager().createFileAtPath(bundlePath + "/" + n, contents: nil, attributes: nil)
116119
}
120+
// Add a resource into a subdirectory
121+
let subDirPath = bundlePath + "/" + _subDirectory
122+
try NSFileManager.defaultManager().createDirectoryAtPath(subDirPath, withIntermediateDirectories: false, attributes: nil)
123+
NSFileManager.defaultManager().createFileAtPath(subDirPath + "/" + _main + "." + _type, contents: nil, attributes: nil)
117124
} catch _ {
118125
return nil
119126
}
@@ -140,6 +147,9 @@ class TestNSBundle : XCTestCase {
140147
XCTAssertNotNil(worldResources)
141148
XCTAssertEqual(worldResources?.count, 2)
142149

150+
let path = bundle?.pathForResource(_main, ofType: _type, inDirectory: _subDirectory)
151+
XCTAssertNotNil(path)
152+
143153
_cleanupPlayground(playground)
144154
}
145155
}

0 commit comments

Comments
 (0)