@@ -28,7 +28,9 @@ public class NSBundle : NSObject {
28
28
public init ? ( path: String ) {
29
29
super. init ( )
30
30
31
- let resolvedPath = path. _nsObject. stringByResolvingSymlinksInPath
31
+ // TODO: We do not yet resolve symlinks, but we must for compatibility
32
+ // let resolvedPath = path._nsObject.stringByResolvingSymlinksInPath
33
+ let resolvedPath = path
32
34
guard resolvedPath. length > 0 else {
33
35
return nil
34
36
}
@@ -136,12 +138,24 @@ public class NSBundle : NSObject {
136
138
}
137
139
138
140
// -----------------------------------------------------------------------------------
139
- // MARK: - URL and Path Resource Lookup
141
+ // MARK: - URL Resource Lookup - Class
140
142
141
- public class func URLForResource( name: String ? , withExtension ext: String ? , subdirectory subpath: String ? , inBundleWithURL bundleURL: NSURL ) -> NSURL ? { NSUnimplemented ( ) }
143
+ public class func URLForResource( name: String ? , withExtension ext: String ? , subdirectory subpath: String ? , inBundleWithURL bundleURL: NSURL ) -> NSURL ? {
144
+ // If both name and ext are nil/zero-length, return nil
145
+ if ( name == nil || name!. isEmpty) && ( ext == nil || ext!. isEmpty) {
146
+ return nil
147
+ }
148
+
149
+ return CFBundleCopyResourceURLInDirectory ( bundleURL. _cfObject, name? . _cfObject, ext? . _cfObject, subpath? . _cfObject) . _nsObject
150
+ }
142
151
143
- public class func URLsForResourcesWithExtension( ext: String ? , subdirectory subpath: String ? , inBundleWithURL bundleURL: NSURL ) -> [ NSURL ] ? { NSUnimplemented ( ) }
152
+ public class func URLsForResourcesWithExtension( ext: String ? , subdirectory subpath: String ? , inBundleWithURL bundleURL: NSURL ) -> [ NSURL ] ? {
153
+ return CFBundleCopyResourceURLsOfTypeInDirectory ( bundleURL. _cfObject, ext? . _cfObject, subpath? . _cfObject) ? . _unsafeTypedBridge ( )
154
+ }
144
155
156
+ // -----------------------------------------------------------------------------------
157
+ // MARK: - URL Resource Lookup - Instance
158
+
145
159
public func URLForResource( name: String ? , withExtension ext: String ? ) -> NSURL ? {
146
160
return self . URLForResource ( name, withExtension: ext, subdirectory: nil )
147
161
}
@@ -151,20 +165,41 @@ public class NSBundle : NSObject {
151
165
if ( name == nil || name!. isEmpty) && ( ext == nil || ext!. isEmpty) {
152
166
return nil
153
167
}
154
- let resultURL = CFBundleCopyResourceURL ( _bundle, name? . _cfObject, ext? . _cfObject, subpath? . _cfObject)
155
- return unsafeBitCast ( resultURL, NSURL . self)
168
+ return CFBundleCopyResourceURL ( _bundle, name? . _cfObject, ext? . _cfObject, subpath? . _cfObject) ? . _nsObject
156
169
}
157
170
158
- public func URLForResource( name: String ? , withExtension ext: String ? , subdirectory subpath: String ? , localization localizationName: String ? ) -> NSURL ? { NSUnimplemented ( ) }
171
+ public func URLForResource( name: String ? , withExtension ext: String ? , subdirectory subpath: String ? , localization localizationName: String ? ) -> NSURL ? {
172
+ // If both name and ext are nil/zero-length, return nil
173
+ if ( name == nil || name!. isEmpty) && ( ext == nil || ext!. isEmpty) {
174
+ return nil
175
+ }
176
+
177
+ return CFBundleCopyResourceURLForLocalization ( _bundle, name? . _cfObject, ext? . _cfObject, subpath? . _cfObject, localizationName? . _cfObject) ? . _nsObject
178
+ }
159
179
160
- public func URLsForResourcesWithExtension( ext: String ? , subdirectory subpath: String ? ) -> [ NSURL ] ? { NSUnimplemented ( ) }
180
+ public func URLsForResourcesWithExtension( ext: String ? , subdirectory subpath: String ? ) -> [ NSURL ] ? {
181
+ return CFBundleCopyResourceURLsOfType ( _bundle, ext? . _cfObject, subpath? . _cfObject) ? . _unsafeTypedBridge ( )
182
+ }
161
183
162
- public func URLsForResourcesWithExtension( ext: String ? , subdirectory subpath: String ? , localization localizationName: String ? ) -> [ NSURL ] ? { NSUnimplemented ( ) }
184
+ public func URLsForResourcesWithExtension( ext: String ? , subdirectory subpath: String ? , localization localizationName: String ? ) -> [ NSURL ] ? {
185
+ return CFBundleCopyResourceURLsOfTypeForLocalization ( _bundle, ext? . _cfObject, subpath? . _cfObject, localizationName? . _cfObject) ? . _unsafeTypedBridge ( )
186
+ }
163
187
164
- public class func pathForResource( name: String ? , ofType ext: String ? , inDirectory bundlePath: String ) -> String ? { NSUnimplemented ( ) }
188
+ // -----------------------------------------------------------------------------------
189
+ // MARK: - Path Resource Lookup - Class
190
+
191
+ public class func pathForResource( name: String ? , ofType ext: String ? , inDirectory bundlePath: String ) -> String ? {
192
+ return NSBundle . URLForResource ( name, withExtension: ext, subdirectory: bundlePath, inBundleWithURL: NSURL ( fileURLWithPath: bundlePath) ) ? . path ?? nil
193
+ }
165
194
166
- public class func pathsForResourcesOfType( ext: String ? , inDirectory bundlePath: String ) -> [ String ] { NSUnimplemented ( ) }
195
+ public class func pathsForResourcesOfType( ext: String ? , inDirectory bundlePath: String ) -> [ String ] {
196
+ // Force-unwrap path, beacuse if the URL can't be turned into a path then something is wrong anyway
197
+ return URLsForResourcesWithExtension ( ext, subdirectory: bundlePath, inBundleWithURL: NSURL ( fileURLWithPath: bundlePath) ) ? . map { $0. path! } ?? [ ]
198
+ }
167
199
200
+ // -----------------------------------------------------------------------------------
201
+ // MARK: - Path Resource Lookup - Instance
202
+
168
203
public func pathForResource( name: String ? , ofType ext: String ? ) -> String ? {
169
204
return self . URLForResource ( name, withExtension: ext, subdirectory: nil ) ? . path
170
205
}
@@ -173,10 +208,19 @@ public class NSBundle : NSObject {
173
208
return self . URLForResource ( name, withExtension: ext, subdirectory: nil ) ? . path
174
209
}
175
210
176
- public func pathForResource( name: String ? , ofType ext: String ? , inDirectory subpath: String ? , forLocalization localizationName: String ? ) -> String ? { NSUnimplemented ( ) }
211
+ public func pathForResource( name: String ? , ofType ext: String ? , inDirectory subpath: String ? , forLocalization localizationName: String ? ) -> String ? {
212
+ return self . URLForResource ( name, withExtension: ext, subdirectory: subpath, localization: localizationName) ? . path
213
+ }
214
+
215
+ public func pathsForResourcesOfType( ext: String ? , inDirectory subpath: String ? ) -> [ String ] {
216
+ // Force-unwrap path, beacuse if the URL can't be turned into a path then something is wrong anyway
217
+ return self . URLsForResourcesWithExtension ( ext, subdirectory: subpath) ? . map { $0. path! } ?? [ ]
218
+ }
177
219
178
- public func pathsForResourcesOfType( ext: String ? , inDirectory subpath: String ? ) -> [ String ] { NSUnimplemented ( ) }
179
- public func pathsForResourcesOfType( ext: String ? , inDirectory subpath: String ? , forLocalization localizationName: String ? ) -> [ String ] { NSUnimplemented ( ) }
220
+ public func pathsForResourcesOfType( ext: String ? , inDirectory subpath: String ? , forLocalization localizationName: String ? ) -> [ String ] {
221
+ // Force-unwrap path, beacuse if the URL can't be turned into a path then something is wrong anyway
222
+ return self . URLsForResourcesWithExtension ( ext, subdirectory: subpath, localization: localizationName) ? . map { $0. path! } ?? [ ]
223
+ }
180
224
181
225
// -----------------------------------------------------------------------------------
182
226
// MARK: - Localized Strings
0 commit comments