Kir*_*ran 56 cocoa-touch objective-c filesize nsfilemanager ios
在我的iPhone应用程序中,我使用以下代码来查找文件的大小.即使该文件存在,我看到的大小为零.谁能帮我?提前致谢.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *URL = [documentsDirectory stringByAppendingPathComponent:@"XML/Extras/Approval.xml"];
NSLog(@"URL:%@",URL);
NSError *attributesError = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];
int fileSize = [fileAttributes fileSize];
Run Code Online (Sandbox Code Playgroud)
Rog*_*ger 125
试试这个;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
Run Code Online (Sandbox Code Playgroud)
请注意,fileSize不一定适合整数(尤其是有符号的整数),尽管你可以在iOS中使用很长时间,因为在现实中你永远不会超过它.该示例使用long long,因为在我的代码中,我必须与具有更大存储空间的系统兼容.
Swift中的一个班轮:
let fileSize = try! NSFileManager.defaultManager().attributesOfItemAtPath(fileURL.path!)[NSFileSize]!.longLongValue
Run Code Online (Sandbox Code Playgroud)
如果您有URL( NSURL,而不是String),则可以在没有 的情况下获取文件大小FileManager:
let attributes = try? myURL.resourceValues(forKeys: Set([.fileSizeKey]))
let fileSize = attributes?.fileSize // Int?
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39128 次 |
| 最近记录: |