tags:

views:

8

answers:

1

I'm using NSBundle resources to load some media:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:someValue ofType@"someType"]];

if there is no resource found, the url value, of course, remains valid and using it would cause application crash, so the simple check

if(!url) { //or even (!mediaPlayer), or (url == nil)
    //show alert box
}

does not help.

A: 

The correct way to do it is to compare the result of pathForResource:ofType: against nil, and handle the error at that point.

NSString *pathString = [[NSBundle mainBundle] pathforResource:@"name" ofType:@"ext"];
if (!pathString) {
   // Handle error
}

NSURL *theURL = [NSURL fileURLWithPath:pathString];
eliego