I made this function that returns the size of file in documents directory, it works but I get warning that I wish to fix, the function:
-(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;
}
*The Warning is 'fileAttributesAtPath:traverseLink: is deprecated first deprecated in ios 2.0'. What is it mean and how I can fix it?
In most cases, when you get a report about a deprecated method, you look it up in the reference docs and it will tell you what replacement to use.
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.)
So use attributesOfItemAtPath:error:
instead.
Here's the simple way:
NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:getFilePath error:nil];
The more complete way is:
NSError *error = nil;
NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:getFilePath error:&error];
if (fileDictionary) {
// make use of attributes
} else {
// handle error found in 'error'
}
Edit: In case you don't know what deprecated means, it means that the method or class is now obsolete. You should use a newer API to perform a similar action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With