Fun with Primitives
Fun with Primitives
- Subject: Fun with Primitives
- From: Owen McKerrow <email@hidden>
- Date: Tue, 13 Dec 2005 16:00:49 +1100
Hi All,
I have a method that loops over a NSMutableDictionary by keyword, and
then uses that keyword as a key value path to set the value of an
object for another page to the value stored in the dictionary.
For example we have the key value pair in the dictionary of
"personName", "Bob".
So I set the value in the next page with the following call
nextPage.takeValueForKeyPath(dictionary.objectForKey(keyName),keyName);
So in this case we set the valueForKeyPath "personName" on the next
page to the value found in the dictionary which matches the same key,
in this case that value would be Bob.
However this breaks down when I have to deal with primitives. A
NSMutableDictionary won't take a primitive ( e.g. an int) as a
possible object, so we cast it to an Integer. However when you then
try and assign this to the int on the next page it of course doesn't
work as the int can't be assigned to an Integer.
So we could check what class type we have for dictionary.objectForKey
(keyName) and if its an Integer, use .intValue(), but that means ALL
Integers would be cast back and this is not what we would want.
Taking that one step further you can check if
nextPage.valueForKeyPath(keyName).getClass().isInstance
(Integer.TYPE), that is checking if you are about to set an int or
not on the nextPage and if so cast the object to an Integer ( Integer
value = (Integer) dictionary.objectForKey(keyName)) and then
use .intValue() for the ones which pass this test However this also
breaks as nextPage.takeValueForKeyPath() needs an Object not a
primitive.
I know I could just write a method that takes an Integer and assign's
it to the int :
setNumber (Integer newNum) {
setNumber(newNum.intValue());
}
But I was hoping to make the method generic enough that it could just
do it, otherwise I would have to write this extra method every-time I
wanted to use it ( which may be the only way to go ).
But before I headed down this path I thought I would ask and see if
anyone has any ideas or suggestions ?
Owen McKerrow
WebMaster, emlab
http://emlab.uow.edu.au
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - -
"As of tomorrow, employees will only be able to access the building
using individual security cards. Pictures will be taken next
Wednesday employees will receive their cards in two weeks."
- "Dilbert Quotes" Winner, Fred Dales, Microsoft Corp
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden