[英]iPhone SDK: how to properly handle errors of NSBundle resource loading?
我正在使用NSBundle資源加載某些媒體:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:someValue ofType@"someType"]];
如果找不到資源,則url值當然仍然有效,使用它會導致應用程序崩潰,因此簡單檢查
if(!url) { //or even (!mediaPlayer), or (url == nil)
//show alert box
}
沒有幫助。
正確的方法是將pathForResource:ofType:的結果與nil進行比較,然后處理該點的錯誤。
NSString *pathString = [[NSBundle mainBundle] pathforResource:@"name" ofType:@"ext"];
if (!pathString) {
// Handle error
}
NSURL *theURL = [NSURL fileURLWithPath:pathString];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.