开发者

Error while using getResourceValue:forKey method in NSURL

开发者 https://www.devze.com 2023-01-11 00:35 出处:网络
I am trying to fetch the file size attribute from a NSURL object by passing it getResourceValue:forKey method. Here is the code i am writing:

I am trying to fetch the file size attribute from a NSURL object by passing it getResourceValue:forKey method. Here is the code i am writing:

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);

But i am getting following error:

Error Domain=NSPOSIXErrorDomain Code=17 "T开发者_开发百科he operation couldn’t be completed. File exists"

Please assist.


What type is name in your example? You should be passing in the address of the variable where you want the size to be stored:

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);


If you are trying to get the file size of a local file, see NSFileManager

For example, to get the size of a file: int size = [[[NSFileManager defaultManager] attributesOfItemAtPath:@"/Users/Abhinav/Test/abhi.pdf" error:NULL] fileSize]; should work (and will set size to 0 if the file does not exist, or is not readable).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号