Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

isharan

macrumors member
Original poster
Jun 3, 2009
52
0
I'm working through Aaron Hillegass' Cocoa Programming for Mac OS X 3rd Edition and have a question on Chapter 8 (NSArrayController) In MyDocument.m, in the dealloc method, instead of releasing the employees array, its instead set to nil. What's the reasoning behind this?

Code:
- (void)dealloc
{
    [self setEmployees:nil];
    [super dealloc];
}
 
The reason this is done is that it's a little safer than outright releasing the object. If you send an explicit release message to an object that's already been deallocated, your app will crash. I know, you're probably thinking "But I should already know if my object is valid or not when dealloc is called!", and you should know, but trust me, you won't when your app gets any kind of complexity.

Generally in your accessor you will check the existence of an object and then release the object before assigning a new one, so if you send nil to the accessor (setter), it will first check for you that your object exists, and then release it for you. You could just duplicate the code in dealloc, but since your accessor probably has it anyway already (make sure!), you might as well reuse it.

Note that you need to be careful here that you're not doing anything else in your setter method, like working on some other objects that may have already been deallocated when called from the dealloc method. Generally a straight setter will look something like this:

Code:
- (void)setName:(NSString *)newName {
	if (name != nil) {
		[name release];
		name = nil;
	}
	name = [newName copy];
}
 
Messaging nil is safe, so that's really not that necessary. Also you want to avoid accidentally triggering KVO notifications while deallocating.
 
You guys conspired to unnecessarily confuse me, so I had to look what the book says.

Setter method is just a few millimeters below -dealloc and there's a comment that should give us a clue:

Hillegass: "This is an unusual setter method. We are going to add a lot of smarts to it in the next chapter."
 
Out of curiosity, does anyone write accessor methods anymore? I haven't come across something I couldn't do with @property/@sythesize

I usually use @property (retain, nonatomic) for the vast majority of objects and @property (assign, nonatomic) for normal datatypes.

I find (retain, nonatomic) works particularly well with convenience methods like [NSString stringWithFormat: @"%0.2f", someDouble]


Off topic. Today I found myself calculating something in a double and then wanting to carry out another computation on fixed precision form of that double, like say to 2 decimal places. So I basically did this.
fixedDouble = [[NSString stringWithFormat: @"%0.2f", someDouble] doubleValue];

Is there an easier way? Assuming I have to put both the someDouble and fixedDouble through some calculations for instance <math.h>'s acos(); function?
 
Is there an easier way? Assuming I have to put both the someDouble and fixedDouble through some calculations for instance <math.h>'s acos(); function?

Multiply the number by 100, round it to a whole number (the choice of round(), trunc(), etc. is up to you), then divide it by 100.

Note that there's no way to "exactly" round a floating-point number to a set number of decimal places, there will often be some error because not all numbers can be represented exactly by a float. For example, 3.10 is actually stored as something like 3.099999904633.

This is also why you should never use floats for monetary quantities.
 
Multiply the number by 100, round it to a whole number (the choice of round(), trunc(), etc. is up to you), then divide it by 100.

Note that there's no way to "exactly" round a floating-point number to a set number of decimal places, there will often be some error because not all numbers can be represented exactly by a float. For example, 3.10 is actually stored as something like 3.099999904633.

This is also why you should never use floats for monetary quantities.

I realize that, but I'm working on something where someone is expected to calculate something and see if they agree on the computer's result. The user will not even be shown the whole double, so in certain cases they would get widely different results than the computer if the computer used the whole number for the calculation. So far it is working perfectly, but I do believe this carries less overhead. So thank you.

Second off topic. Say I need to pause my iPhone app for say 400ms or so while something is displayed, then trigger a method. How would I setup this seemingly easy delay?
 
Second off topic. Say I need to pause my iPhone app for say 400ms or so while something is displayed, then trigger a method. How would I setup this seemingly easy delay?
I would use NSObject's performSelector:withObject:afterDelay: I'm not sure this is available on iPhone, but I assume it is.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.