NSFileManager and file sizes help

Discussion in 'Mac Programming' started by stu02, Oct 13, 2006.

  1. stu02 macrumors newbie

    Joined:
    Aug 1, 2006
    Location:
    Edinburgh, Scotland
    #1
    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:

    Code:
    - (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:

    Code:
    NSDictionary *infoDict = [[NSFileManager defaultManager] fileSystemAttributesAtPath:[panel filename]];
    
    then setBackupFileSize is called by:

    Code:
    [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
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    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.
     
  3. stu02 thread starter macrumors newbie

    Joined:
    Aug 1, 2006
    Location:
    Edinburgh, Scotland
    #3
    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
     
  4. mindwalkernine macrumors newbie

    Joined:
    Jul 23, 2006
    #4
    Code:
    - (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.
     
  5. stu02 thread starter macrumors newbie

    Joined:
    Aug 1, 2006
    Location:
    Edinburgh, Scotland
    #5
    Yeah I know I realized that in my above post and corrected the method to simply be:

    Code:
    - (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
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    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.
     
  7. stu02 thread starter macrumors newbie

    Joined:
    Aug 1, 2006
    Location:
    Edinburgh, Scotland
    #7
    I using the two very basic ones which are:

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

    Code:
    - (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
     
  8. mindwalkernine macrumors newbie

    Joined:
    Jul 23, 2006
    #8
    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
     
  9. stu02 thread starter macrumors newbie

    Joined:
    Aug 1, 2006
    Location:
    Edinburgh, Scotland
    #9
    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
     

Share This Page