New version
In iOS app I need to read several text files that are placed in resource bundle. I created following test code in class MyClass
:
private let bundle = NSBundle(forClass: MyClass.self)
private func testLoadResource() {
var error: NSError? = NSError()
for fileUrl in NSFileManager.defaultManager().contentsOfDirectoryAtURL(bundle.resourceURL!,
includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.allZeros, error: &error)! {
print("\(NSFileManager.defaultManager().fileExistsAtPath((fileUrl as! NSURL).absoluteString!)), ")
}
}
The console output of this code is:
false, false, false, false, false, false, false, false, false, false, false
So the files found by NSFileManager.defaultManager().contentsOfDirectoryAtURL
do not exist according to NSFileManager.defaultManager().fileExistsAtPath
?! How is that possible?
Apparently, resource files that I put in resource bundle can by found by contentsOfDirectoryAtURL
but not by fileExistsAtPath
function of NSFileManager
. And I also cannot find the way how to read them. Please help.
Also note, that both the class MyClass
(with testing code above) and the resource text files I need to read are located in iOS framework.
Original question
In my iOS app need to read several text files that I put in resource bundle. However, I have great trouble with reading those files from code. I created following test code:
private let bundle = NSBundle(forClass: MyClass.self)
private func testLoadResource() {
var error: NSError? = NSError()
if let k = NSFileManager.defaultManager().contentsOfDirectoryAtURL(bundle.resourceURL!, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.allZeros, error: &error) {
for i in k {
println(String(contentsOfFile: (i as! NSURL).absoluteString!, encoding: NSUTF8StringEncoding, error: &error))
println(error)
}
}
}
For all the text files I put in the bundle the above code produces following error output:
Optional(Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)"
UserInfo=0x17407aa40 {NSFilePath=file:///private/var/mobile/Containers/Bundle/Application/73BEA20A-FC36-46EB-B27B-2124FF24BAFD/EasyType.app/Frameworks/EasyTypeShared.framework/blurredTest.text,
NSUnderlyingError=0x17404e5b0 "The operation couldn’t be completed. No such file or directory"})
blurredTest.text
is name of the file I placed in the bundle. The file apparently exists because its URL is returned by contentsOfDirectoryAtURL
for folder bundle.resourceURL!
but it the error says it does not exist! Also testing for existence with NSFileManager.defaultManager().fileExistsAtPath(i)
returns false
.
Please not that the text file blurredTest.text
is located in iOS framework as well as reading code in class MyClass
(above).
So how do I read that file?
The array you get back from
NSFileManager
'scontentsOfDirectoryAtURL
are strings that contain the name of each file in the directory of the passed in path. If for example your bundle path was/MyGreatApp/Resources/
and you had a file namedSomeText.txt
in it the array that came back fromcontentsOfDirectoryAtURL
would beSomeText.txt
, not/MyGreatApp/Resources/SomeText.txt
. As a result when you grab an item from the array and ask the file manager if it exists it looks at/SomeText.txt
instead of/MyGreatApp/Resources/SomeText.txt
where the file actually is.If you take each result in the array you get from
contentsOfDirectoryAtURL
and append it to the end of the bundle url you should see much better results.