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

I made this function, which returns the file size in the document directory, it works, but I get a warning that I want 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; } 

* Warning: fileAttributesAtPath: traverseLink: deprecated, deprecated in ios 2.0. What does this mean and how can I fix it?

+7
source share
2 answers

In most cases, when you get a report on an obsolete method, you look at it in the reference documents and it will tell you which replacement to use.

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

Therefore use attributesOfItemAtPath:error:

Here is an easy way:

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

A 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: if you don't know what deprecated means, it means the method or class is deprecated. You must use the new API to perform a similar action.

+8
source

The accepted answer forgot to process traverseLink:YES from the question.

The improved answer uses both attributesOfItemAtPath:error: and stringByResolvingSymlinksInPath :

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

All Articles