PDA

View Full Version : NSFileManager and file sizes help




stu02
Oct 13, 2006, 08:27 AM
I'm relatively new to Cocoa and Objective-c and I'm currently developing my first application, which is a simple backup utility.

I'm quite pleased at the progress I've made but I've run into a problem I can't seem to figure out regarding the sizes of the files/folders which are to backed up.

The way the application works is that whenever the user adds a file/folder to be backed up an object of my custom class type is added to an array. My custom class is extremely simple as it only holds 4 pieces of information regarding the file, which are the filename, it's path, it's icon and it's size.

Everything woks fine except setting the size of the file/folder using its accessor method. The method used is:


- (void)setBackupFileSize:(unsigned long long)bytesSize
{
if (backupFileSize != bytesSize) {
[backupFileSize release];
backupFileSize = [bytesSize retain];
}
}


The file size passed in the above method is determined by the following:


NSDictionary *infoDict = [[NSFileManager defaultManager] fileSystemAttributesAtPath:[panel filename]];


then setBackupFileSize is called by:


[newFile setBackupFileSize:[[NSNumber numberWithLongLong:[infoDict fileSize]] unsignedLongLongValue]];


where newFile is of my custom class type.

The problem I'm having is that I'm getting the following compiler warnings which either set the file/folder size to 0 at runtime or it just crashes the app.

The warnings are:

warning: cast to pointer from integer of different size
warning: invalid receiver type 'long long unsigned int'
warning: assignment makes integer from pointer without a cast

which are all for the 3rd and 4th lines in the setBackupFileSize method.

This has been driving me nuts so any help would be greatly appreciated.

Stu



HiRez
Oct 13, 2006, 11:03 AM
It looks like you may be trying to compare a primitive type (bytesSize) with an object (backupFileSize)? Maybe try if ([backupFileSize longLongValue] != bytesSize)? I'd also recommend using [bytesSize copy] instead of [bytesSize retain] in your accessor. I usually copy small objects instead of retaining them, as it can eliminate some nasty bugs.

stu02
Oct 13, 2006, 11:26 AM
Thanks for the help but trying your suggestion just gives me another 2 warnings regarding the start of the 'if' statement. I'm really struggling to understand why my original method doesn't work as backupFileSize is declared as an instance variable of the class as being of type unsigned long long so the comparison shouldn't be a problem.

EDIT: Well, I got rid of the warnings after thinking about what you said and then I realized that I was trying to retain and release the variable as if it were an object where as its a primitive type like you said. Except now the app is crashing whenever I try and add a new file/folder to be backed up.

Stu

mindwalkernine
Oct 17, 2006, 12:23 AM
- (void)setBackupFileSize:(unsigned long long)bytesSize
{
if (backupFileSize != bytesSize) {
[backupFileSize release];
backupFileSize = [bytesSize retain];
}
}


backupFileSize is being used as a primitive type as well as a pointer to an instance of a class. It is not clear from this snippit whether it's one or the other.

You need two variables: a primitive variable for your backupFileSize and another for a pointer to your object.

stu02
Oct 17, 2006, 06:19 AM
Yeah I know I realized that in my above post and corrected the method to simply be:


- (void)setBackupFileSize:(unsigned long long)bytesSize
{
if (backupFileSize != bytesSize) {
backupFileSize = bytesSize;
}
}


That did resolve the warnings except now it still crashes whenever I try and add a new file to be backed up. I have a suspicion that it may be the table view thats crashing the app as I'm calling [tableView reloadData] after adding the file so the table reflects the array.

Having said that I'm not aware that NSTableView had any constraints on the data it displayed.

Thanks anyway.

Stu

kainjow
Oct 17, 2006, 01:57 PM
Well post your table view delegate methods here and we'll take a look.

Remember that NSTableView can only use objects, so if you have a primitive, make sure you wrap it in an NSNumber.

stu02
Oct 17, 2006, 05:27 PM
I using the two very basic ones which are:


- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [activeSet count];
}


and


- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
NSString *identifier = [aTableColumn identifier];

BUFile *file = [activeSet objectAtIndex:rowIndex];

return [file valueForKey:identifier];
}


and some other ones which are for column sorting and stuff which I'm pretty sure aren't causing the problem.

I'm really starting to wonder why its crashing all the time while all the methods seem to fine. Before it was because of the file size being a primitive type but now that I've wrapped it in an NSNumber and added an NSNumberFormatter its still acting up even if I comment out that part of the code. Its also displaying some very strange behaviour, for example allowing me to add a couple of files to the table and setting the file size to 0 but then crashing as soon as I click the button to add another.

I've tried stepping through all the code using the debugger and it seems to be crashing just after adding the file to the table, i.e just as it finishes setting the values of the columns.

Anyway thanks for the help I really appreciate it.

Stu

mindwalkernine
Oct 18, 2006, 12:33 AM
I can't tell much from this snippit, but if you don't mind emailing me your project, I would take a look. I have used NSTableView before and got it to work as well as write files successfully.

Sounds like you app is basic, so it should be too hard to dive into.

mindwalkernine@gmail.com

stu02
Oct 18, 2006, 12:35 PM
Yeah sure that would be great, and yeah the app is pretty basic as its my first one but it would be great to have someone with a lot more experience than me with cocoa to look it over and point out the areas where I'm going wrong.

Thanks I really appreciate it.

Stu