Thursday, May 9, 2013

NSDictionary NSFileSize example ios

fileSize - NSFileSize

Returns the value for the key NSFileSize.
- (unsigned long long)fileSize
Return Value
The value, as an unsigned long long, for the key NSFileSize, or 0 if the dictionary doesn’t have an entry for the key.
Discussion of [NSDictionary NSFileSize]
This and the other file... methods are for use with a dictionary such, as those returned from the methodsfileAttributesAtPath:traverseLink: (NSFileManager), directoryAttributes(NSDirectoryEnumerator), and fileAttributes (NSDirectoryEnumerator), that represents the POSIX attributes of a file or directory. This method returns the file’s size.
Special Considerations
If the file has a resource fork, the returned value does not include the size of the resource fork.
Example of [NSDictionary NSFileSize]
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];

NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
Example of [NSDictionary NSFileSize]
directoryContent = [[NSMutableArray alloc] init];
    for (NSString *path in paths){
filesDictionary  =[[NSMutableDictionary alloc] init];
filesSize = [[NSNumber alloc] init]; 
filesSize = [filesDictionary objectForKey:NSFileSize];
filesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:filesSize, @"filesSize", nil];
[directoryContent addObject:[filesDictionary copy]];
Example of [NSDictionary NSFileSize]
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *persistentStorePath = [documentsDirectory stringByAppendingPathComponent:@"database.sqlite"];

NSError *error = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:persistentStorePath error:&error];
NSLog(@"Persistent store size: %@ bytes", [fileAttributes objectForKey:NSFileSize]);

NSDictionary *fileSystemAttributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:persistentStorePath error:&error];
NSLog(@"Free space on file system: %@ bytes", [fileSystemAttributes objectForKey:NSFileSystemFreeSize]);