File Size Search

In my iPhone application, I use the following code to find the file size. Despite the fact that the file exists, I see zero for size. Can someone help me? Thanks in advance.

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]; 
+53
ios objective-c cocoa-touch filesize nsfilemanager
Apr 21 '11 at 12:08
source share
2 answers

Try it;

 NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError]; NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize]; long long fileSize = [fileSizeNumber longLongValue]; 

Please note that fileSize will not necessarily fit into an integer (especially signed), although you can certainly refuse to use iOS, because in reality you will never exceed it. The example lasts a long time since in my code I have to be compatible with systems with much larger storage.

+118
Apr 21 '11 at 12:16
source share

One liner in Swift:

 let fileSize = try! NSFileManager.defaultManager().attributesOfItemAtPath(fileURL.path!)[NSFileSize]!.longLongValue 
+6
Apr 22 '16 at 7:02
source share



All Articles