2012-06-21 4 views
1

원인 NSFileManager를 사용하여 파일 읽기 : 오류의 원인오류 나는 다음과 같은 오류를 얻고있다

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' 
*** First throw call stack: 

코드 것은 다음 plistData가 null이기 때문에

NSDictionary *dict = nil; 
    @synchronized(self) { 
     dict = [pendingDictionarySaves objectForKey:file]; 
    } 
    if (dict) return dict; 

    @synchronized(self) { 
     dict = [savedDictionaries objectForKey:file]; 
    } 
    if (dict) return dict; 

    NSData *plistData = [[NSFileManager defaultManager] contentsAtPath:file]; 

    NSError *error = nil; 
    if ([[NSPropertyListSerialization class] 
     respondsToSelector:@selector(propertyListWithData:options:format:error:)]) { 
     return [NSPropertyListSerialization propertyListWithData:plistData 
                 options:NSPropertyListImmutable 
                  format:NULL 
                  error:&error]; 
    } 

는 기본적으로 이유입니다. 파일 경로는 다음과 같습니다

/Users/aditya15417/Library/Application Support/iPhone Simulator/5.1/Applications/A355D003-668C-4B81-80DC-66C968FE57D3/Library/Caches/NewsfeedCache/?type=news 

여기에 내가 경로를 초기화하는 방법은 다음과 같습니다

NSString *path = [basePath stringByAppendingPathComponent:[streamURL uniqueFileName]]; 

    // Create the directories if needed 
    if (![[NSFileManager defaultManager] fileExistsAtPath:path]) { 
     [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil]; 
    } 

    return path; 

문제는 그것이 가능하다이 pListData가 null의 경우 확인하지 않고 작동하도록하는 방법은?

+0

이제 문제가 무엇인지 알았습니다. 무엇이 문제입니까? –

+0

basePath 란 무엇입니까? – user523234

+0

파일 경로 앞에 실제 파일 이름이없는 URL 매개 변수가 파일 경로에있는 이유는 무엇입니까? – sosborn

답변

0

아마도 [+ NSData dataWithContentsOfFile : options : error :]를 사용하는 것이 더 나을 것입니다. 이 메소드는 읽기가 실패 할 경우 최소한 오류를 줄 것이다. 다음과 같음 :

NSError *error = nil; 
NSData *plistData = [NSData dataWithContentsOfFile:file options:0 error:&error]; 
if(!plistData) { 
    NSLog(@"failed to read data: %@",error); 
    return nil; 
} 

if ([[NSPropertyListSerialization class] 
    respondsToSelector:@selector(propertyListWithData:options:format:error:)]) { 
    return [NSPropertyListSerialization propertyListWithData:plistData 
                options:NSPropertyListImmutable 
                 format:NULL 
                 error:&error]; 
} 
관련 문제