2010-08-12 6 views
0

NSURL 개체에서 getResourceValue : forKey 메서드를 전달하여 파일 크기 특성을 가져 오려고합니다. 여기에 코드를 내가 쓰고 있습니다 :getResourceValue : forKey 메서드를 NSURL에서 사용하는 동안 오류가 발생했습니다.

NSURL *samplePath = [[NSURL alloc] initWithString:@"file://localhost/Users/Abhinav/Test/abhi.pdf"]; 
[samplePath getResourceValue:&name forKey:NSURLFileSizeKey error:&error]; 
NSLog(@"Error===%@",error); 
NSLog(@"name===%@",name); 

그러나 나는 다음과 같은 오류가 점점 오전 :

Error Domain=NSPOSIXErrorDomain Code=17 "The operation couldn’t be completed. File exists" 

지원하시기 바랍니다. 로컬 파일의 파일 크기를 얻으려고 노력하는 경우

답변

1

, 파일의 크기를 얻기 위해, 예를 들어 NSFileManager

참조 : int size = [[[NSFileManager defaultManager] attributesOfItemAtPath:@"/Users/Abhinav/Test/abhi.pdf" error:NULL] fileSize];0size 작동합니다 (및 설정합니다 파일이하지 않는 경우 존재하거나 읽을 수없는 경우).

+0

이것은 정말 도움이되었습니다. – Abhinav

5

예를 들어 어떤 유형이 name입니까? 크기를 저장하려는 변수의 주소를 전달해야합니다.

NSURL *samplePath = [[NSURL alloc initWithString:@"file://localhost/Users/Abhinav/Test/abhi.pdf"]; 
NSNumber *fileSizeBytes; 
[samplePath getResourceValue:&fileSizeBytes forKey:NSURLFileSizeKey error:&error]; 
NSLog(@"Error===%@",error); NSLog(@"size of file in bytes ===%@",fileSizeBytes); 
관련 문제