Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning 'fileAttributesAtPath:traverseLink is deprecated: first deprecated in ios 2.0

Tags:

xcode

ios

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?

like image 904
DanM Avatar asked Nov 08 '12 16:11

DanM


1 Answers

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.

like image 157
rmaddy Avatar answered Nov 15 '22 16:11

rmaddy