I need to get size of video file with NSFileManager. I can retrieve URL path of my file. I found Objective-C code which seems working but my project is developed with Swift. How can I write the following code with Swift?
NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
//Error Container
NSError *attributesError;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[videoUrl path] error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
                Try this code:
let videoUrl = info[UIImagePickerControllerMediaURL] as! NSURL
var attributesError: NSError?
let fileAttributes = NSFileManager.defaultManager().attributesOfItemAtPath(videoURL.path!, error: &attributesError)!
let fileSizeNumber = fileAttributes[NSFileSize] as! NSNumber
let fileSize = fileSizeNumber.longlongValue
                        For SWIFT 2 try:
let videoUrl = info[UIImagePickerControllerMediaURL] as! NSURL
do{
     let fileAttributes = try NSFileManager.defaultManager().attributesOfItemAtPath(videoURL.path!) 
     let fileSize = fileAttributes[NSFileSize]      
}
catch let err as NSError{
     //error handling
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With