iPhone SDK: how to properly handle errors of NSBundle resource loading?
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.
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];
精彩评论