URLForResource:withExtension: method always returns nil - ios-4.2

I am testing the MixerHost sample code. However the following code:
NSURL *beatsLoop = [[NSBundle mainBundle] URLForResource: #"beatsMono"
withExtension: #"caf"];
the beatsLoop is nil.
What's the reason for that?
Should I first create the beatsMono.caf file and then put into some specific path?
Any comments and solutions will be highly appreciated.
Thanks,
finspoo

Should I first create the beatsMono.caf file and then put into some specific path?
Yes. The file beatsMono.caf (case sensitive!) must exist in your application bundle for that method to succeed. This is done by adding the file to the target as a resource in XCode, you cannot do it at runtime.

Related

pathForDirectory method for NSBundle?

If I include a folder in my bundle (a real folder, the blue ones, not the yellow groups), how can I get the path for that folder from my bundle? The method I would usually use is...
[[NSBundle mainBundle] pathForResource:fileName ofType:______];
...but what "type" is a directory? Is there a "type" to use, or is there another method for accessing the paths of directories within the bundle?
Or am I going about this all wrong, and there's some other way for including folders of accessible documents in the bundle?
Directories can have extensions too. If yours doesn't have one, just pass #"" for the type parameter, -[NSBundle pathForResource:ofType:] works for directories too, not only files. At the end, a directory is also a resource :)
As per a suggestion in the comments, it turns out you can use an empty string to refer to folders. So a folder called "myFolder" would be accessible using...
[[NSBundle mainBundle] pathForResource:#"myFolder" ofType:#""];
Hope this helps anyone else who wasn't expecting it to be so easy...
As you're adding a folder by your own, you know the name of the folder, so you can get the path like this:
NSString *myDirectoryName = #"myDirectory";
NSString *absolutePathToMyDirectory = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:myDirectoryName];
You'll have to test if this path exist with NSFileManager.
Otherwise you can still use the method you're using, the type can be nil, so you can call it like this
NSString *absolutePathToMyDirectory = [[NSBundle mainBundle] pathForResource:myDirectoryName ofType:nil];
and then testing if the result is nil or not.
if(absolutePathToMyDirectory) {
// do stuff related to this path
}

NSManagedObjectModel is nil

enter code hereI am working on an import tool, following the pattern objc.io did at https://github.com/objcio/issue-4-importing-and-fetching .
At an early point in the program, where I am creating an instance of NSManagedObjectModel from a url, it is returning nil. When I run the program, the error I get later is "Cannot create an NSPersistentStoreCoordinator with a nil model".
Attached is a screenshot at a breakpoint after I create the model. I've looked around here, and have verified that:
HistologyDataImporter.xcdatamodeld belongs to HistologyDataImporter (target membership)
The NSString I'm using the build the path matches the name of the model file ("HistologyDataImporter")
I have also tried passing "mom" as the extension to URLByAppendingPathComponent, but the model is still nil when run.
Why is [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] still returning nil?
Because you're using the wrong location. What you have is:
NSString *path = #"HistologyDataImporter";
NSURL *modelURL = [NSURL fileURLWithPath:path isDirectory:NO];
moelURL = [modelURL URLByAppendingPathComponent:#"momd"];
If you call fileURLWithPath: with a relative path like this, it's assumed to be relative to the current working directory. Your code does not set the working directory, but the value for modelURL suggests it's somewhere in Xcode's derived data folder. That's not surprising when running the app from Xcode, but the model file is not in that directory.
You need to figure out where HistologyDataImporter.momd is located and make sure that modelURL actually points to that location. If you put the model file on your desktop, you could use the same desktopURL that you have in main() and add the model filename to that.

How can I get full path of a directory (not a file) in developing ios

I am using cocos2d-x and I found it used pathForResource to get full path for a existing file. But I need to get the full path of a directory. If I simply pass "" to pathForResource, it will search and return the first file in that directory.
I cannot just take application directory and put it in front of relative path since I don't know if it's a relative one or already a full one.
I can trim the filename but I think that's a weird solution.
So is there any function in objective-c that works like pathForResource but don't really search for files... just return the directory name
BTW, I am using opendir functions in dirent.h. I found it won't work if I just pass a relative path, which was fine under windows.
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"DirectoryName"];
NSLog(#"%#", sourcePath);

NSBundle creates files for given path

When I use
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:#"sqlite"];
The NSBundle class, instead of returning nil, for a given file name when the file is not included in Xcode, seems to be creating the file no matter what. I tried removing file from xcode, it did not work, then I went to the path for simulator for the project (that path is generated by pathForResource) :
/Users/USER_NAME/Library/Application
Support/iPhoneSimulator/6.0/Applications/APP_CODE/APP_NAME.app/sampleDB.sqlite
Before I run program in the debugger, I delete file "sampleDB.sqlite" manually. Every time the method pathForResource is called the file seems to magically reappear in the folder.
The documentation states :
Return type :
The full pathname for the resource file or nil if the file could not be located.
EDIT:
With a pathForResource commented out, files removed - I still get that file magically reappearing in the bundle. Any Ideas?
Make a clean build to make sure that the file gets removed from the app bundle.

Not able to read file with absolute path (to app bundle)

I am trying to read files from the App bundle (and Library) directory using absolute path like this:
NSString* imagePath = "/var/mobile/Applications/7AC2295E-2775-41EA-B017-AB4048A09F0C/My.app/image.jpg";
When I check if the file exist using
[[NSFileManager defaultManager] fileExistsAtPath:imagePath];
it returns TRUE but when I try to open the image with
[UIImage imangeNamed:imagePath];
it returns nil.
What am I doing wrong here. I am scratching my head for few hours!! please help.
Read the documentation for imageNamed:. It takes a name, not a full path, and searches the application bundle for it. If you want to use a full path you should use another initializer method, such as initWithContentsOfFile:.

Resources