Question on two methods


#1

In the chapter 19, these method shows up in Asset.m

[code]-(NSString *)description
{
return [NSString stringWithFormat:@"<%@: $%d>", [self label], [self resaleValue]];
}

-(void)dealloc
{
NSLog(@“DeallocatingFromAsset.M %@”, self);
}[/code]

and these on Employee.m

[code]-(NSString *)description
{
return [NSString stringWithFormat:@"<employee %d: $%d in assets>", [self employeeID], [self valueOfAssets]];
}

-(void) dealloc
{
NSLog(@“deallocatingFromEmployee.M %@”,self);
}[/code]

But the method wasn’t called main.m.

So here’s my question:
Why did it show up in the debug area when running the program when it wasn’t called in main.m?


#2

They show up because you are overriding dealloc. Dealloc takes place all the time, but there is no NSLog output accociated with it.

So Aaron has you override the dealloc method with a description to show you that the objects are actually getting deallocated when they have no owners left.

That is my understanding of it anyhow. Still new to the language. On chapter 32 at the moment. :smiley:


#3

Thanks for the reply.

So how does overriding work?

Also when I comment out this code in Employee.m

-(NSString *)description { return [NSString stringWithFormat:@"<Employee %d: $%d in assets>", [self employeeID], [self valueOfAssets]]; }

I get this in the debug area.

2012-09-26 18:13:54.040 BMITime[2889:707] Employees: ( "<Employee: 0x1001147f0>", "<Employee: 0x1001148f0>", "<Employee: 0x100114930>", "<Employee: 0x100114970>", "<Employee: 0x100114990>", "<Employee: 0x100114950>", "<Employee: 0x1001149e0>", "<Employee: 0x100114a50>", "<Employee: 0x100114a70>", "<Employee: 0x100114a90>" )

Why is that?


#4

NSObject implements the -description method to return nothing more than the object’s class name and its memory address (using the self variable).

Several other classes that subclass NSObject override -description to be more useful. For example, NSString overrides -description to return the string’s contents. NSDate overrides -description to return a string containing the date in a pretty-formatted way, based on the user’s locale.

If you want more useful descriptions of your objects when you log them using NSLog(), you can override -description to log out whatever string you feel best describes the object.


#5

This might help those who will read this thread in the future.

mobiledevelopertips.com/cocoa/ov … ethod.html