2012-11-08 2 views
7

나는 문서 디렉토리에있는 파일의 크기를 반환하는이 기능을 만들어, 그것을 작동하지만 난 함수 수정 할 것을 경고 얻을 :경고 'fileAttributesAtPath : traverseLink이되지 않습니다 : 첫째 IOS에서 사용되지 않는 2.0

-(unsigned long long int)getFileSize:(NSString*)path 
{ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *getFilePath = [documentsDirectory stringByAppendingPathComponent:path]; 

NSDictionary *fileDictionary = [[NSFileManager defaultManager] fileAttributesAtPath:getFilePath traverseLink:YES]; //*Warning 
unsigned long long int fileSize = 0; 
fileSize = [fileDictionary fileSize]; 

return fileSize; 
} 

* 경고는 'fileAttributesAtPath : traverseLink : ios 2.0에서 처음 사용되지 않음'입니다. 그것은 무엇을 의미하며 어떻게 고칠 수 있습니까?

+1

가능한 복제 [? fileAttributesAtPath 경고와 문제를 해결하는 방법 (http://stackoverflow.com/questions/9019353/how-to- resolve-issues-with-fileattributesatpath-warning) –

답변

8

대부분의 경우 사용되지 않는 메소드에 대한 보고서를 얻으면 참조 문서에서 조회하여 사용할 대체 코드를 알려줍니다.

fileAttributesAtPath:traverseLink: Returns a dictionary that describes the POSIX attributes of the file specified at a given. (Deprecated in iOS 2.0. Use attributesOfItemAtPath:error: instead.)

대신 attributesOfItemAtPath:error:을 사용하십시오.

NSError *error = nil; 
NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:getFilePath error:&error]; 
if (fileDictionary) { 
    // make use of attributes 
} else { 
    // handle error found in 'error' 
} 

편집 :

NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:getFilePath error:nil]; 

더 완벽한 방법은 다음과 같습니다

여기 간단한 방법은 수단이되지 않는 모르는 경우,이 의미하는 방법 또는 클래스는 이제 폐기되었습니다. 새로운 API를 사용하여 유사한 작업을 수행해야합니다.

+0

attributesOfItemAtPath를 사용하는 방법에 대한 예제를 제공해 주시겠습니까? 오류 : – DanM

+0

이것은 실제로 사용하고있는 것과 동일합니다. 빨리 시작하려면'error :'매개 변수에'nil'을 전달하면됩니다. – rmaddy

+1

'attributesOfItemAtPath : error :'는 심볼릭 링크를 지원하지 않습니다. 그래서 당신의 코드는 질문에서'traverseLink : YES'와 같은 행동을하지 않습니다. –

1

허용 대답은 질문에서 traverseLink:YES을 처리하는 것을 잊었습니다.

개선 된 대답은 모두 attributesOfItemAtPath:error:stringByResolvingSymlinksInPath을 사용하고 있습니다 :

NSString *fullPath = [getFilePath stringByResolvingSymlinksInPath]; 
NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil]; 
+1

이것은받은 대답보다 훨씬 낫습니다! –

관련 문제