Finding file's size
Solution 1:
Try this;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
Note that the fileSize won't necessarily fit in an integer (especially a signed one) although you could certainly drop to a long for iOS as you'll never exceed that in reality. The example uses long long as in my code I have to be compatible with systems with much larger storage available.
Solution 2:
One liner in Swift:
let fileSize = try! NSFileManager.defaultManager().attributesOfItemAtPath(fileURL.path!)[NSFileSize]!.longLongValue
Solution 3:
If you have a URL
(NSURL
, not a String
), you can get the file size without a FileManager
:
let attributes = try? myURL.resourceValues(forKeys: Set([.fileSizeKey]))
let fileSize = attributes?.fileSize // Int?