Hello,
I've written methods that help me get the size of files/folders and translate the result in a human readable string. Problem is, when this size exceeds about 2.1GB, the number returned changes to a random negative number, like "-4324234423 bytes", which is useless.
Things I've found out about & done about this issue:
- 32GB is limited to this size, so I compile in 64bit instead.
- I've tried using both CGFloat and NSUInteger, but both still return the same value as NSInteger.
I am quite frustrated, I don't know what I am missing. Here are my methods:
- (NSString *)stringFromFileSize:(int)theSize
{
CGFloat floatSize = theSize;
if (theSize<1023)
return([NSString stringWithFormat:@"%i bytes",theSize]);
floatSize = floatSize / 1024;
if (floatSize<1023)
return([NSString stringWithFormat:@"%1.1f KB",floatSize]);
floatSize = floatSize / 1024;
if (floatSize<1023)
return([NSString stringWithFormat:@"%1.2f MB",floatSize]);
floatSize = floatSize / 1024;
return([NSString stringWithFormat:@"%1.2f GB",floatSize]);
}
- (NSUInteger)sizeOfFile:(NSString *)path
{
NSDictionary *fattrib = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
NSUInteger fileSize = (NSUInteger)[fattrib fileSize];
return fileSize;
}
- (NSUInteger)sizeOfFolder:(NSString*)folderPath
{
NSArray *contents;
NSEnumerator *enumerator;
NSString *path;
contents = [[NSFileManager defaultManager] subpathsAtPath:folderPath];
enumerator = [contents objectEnumerator];
NSUInteger fileSizeInt = 0;
while (path = [enumerator nextObject]) {
NSDictionary *fattrib = [[NSFileManager defaultManager] attributesOfItemAtPath:[folderPath stringByAppendingPathComponent:path] error:nil];
fileSizeInt +=[fattrib fileSize];
}
return fileSizeInt;
}
What am I missing? Is NSFileManager returning a 32bit value? What's causing this?
Thanks!