警告’fileAttributesAtPath:不推荐使用traverseLink:首先在ios 2.0中弃用

我做了这个函数,返回文件目录中的文件大小,它工作,但我收到警告,我希望修复,功能
-(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;
}

*警告是’fileAttributesAtPath:traverseLink:在ios 2.0中首先弃用’.它是什么意思以及我如何解决它?

解决方法

在大多数情况下,当您收到有关已弃用方法的报告时,您可以在参考文档中查找它,它会告诉您要使用的替换项.

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

所以使用attributesOfItemAtPath:error:相反.

这是一个简单的方法

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

更完整的方式是:

NSError *error = nil;
NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:getFilePath error:&error];
if (fileDictionary) {
    // make use of attributes
} else {
    // handle error found in 'error'
}

编辑:如果您不知道弃用的含义,则意味着该方法或类现在已过时.您应该使用较新的API来执行类似的操作.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...