Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting file size with NSFileManager

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];
like image 248
user1951145 Avatar asked Dec 01 '22 00:12

user1951145


2 Answers

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
like image 79
asifmohd Avatar answered Dec 10 '22 10:12

asifmohd


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
}
like image 36
Peter Kreinz Avatar answered Dec 10 '22 11:12

Peter Kreinz