Description method on valueForkey:



I was wondering something. I tried to get the NSNumber from voltage after i used key-value-coding to set it. Now, this returns an NSNumber object, so i wanted to use the description method to see the value:

And it works great, it prints out 240.

Now, which description method is used here? Coz we overwrote the description method in appliance.m. But the valueForKey is calling the description method from it’s own class, right? It is declared in NSKeyValueCoding.h, so if i wanted to overwrite the description method i would have to access that file and change the description method there, or?

Coz i though the message call went like this: we send the message valueForKey: to the instance that a points to. This method goes up the hierarchy till it finds the valueForKey method in the NSKeyValueCoding class. Then it returns an NSNumber object by getting the value from a’s instance variable voltage, and then returns a pointer to that NSNumber. When we then use the description method, i thought it said the description method on a was being used, thus “self” being appliance. But this isn’t the case i see.

Anyone care to enlighten me? i probably just got something backwards, and i’m sorry if this seems confusing, but hey, try being my mind for a day :=)

Thanks on advance!

Best Regards


NSNumber’s description method is being used.


Thank you, so i would need to access the NSNumber.m file and change the description method there, correct?

It uses the NSNumber description method, coz the returned type is an NSNumber, which then means that it uses its own (self) description method, right?

Think i got it if the above is correct, that you Aaron!


You have it right. (But, Apple keeps NSNumber.m in a vault – is compiled into a binary form and linked into the Foundation framework)


After adding the explicit accessor method for setVoltage:, I get the following output:

2013-04-03 14:27:55.313 Appliances[44072:a0f] Setting voltage to 0
2013-04-03 14:27:55.316 Appliances[44072:a0f] a is <Unknown: 120 volts>
2013-04-03 14:27:55.317 Appliances[44072:a0f] Setting voltage to 120
2013-04-03 14:27:55.317 Appliances[44072:a0f] a is <Washing Machine: 240 volts>
2013-04-03 14:27:55.318 Appliances[44072:a0f] The product name is Washing Machine

Why is voltage being set to 0 initially when the voltage is set to 120 in initWithProductName:?