Re: Fun with Primitives
Re: Fun with Primitives
- Subject: Re: Fun with Primitives
- From: email@hidden
- Date: Tue, 13 Dec 2005 09:39:57 +0100
Owen,
Key-value coding takes care of type
coercion.
takeValueForKey("myNumber",
1) may call either setMyNumber(Integer aNumber) or setMyNumber(int anInt).
Your example should work just fine with
only one setter method defined.
Pierre
webobjects-dev-bounces+pierre.bernard=email@hidden
wrote on 12/13/2005 06:00:49 AM:
> 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
>
**********************************************************************
This email and any files transmitted with it are intended solely for
the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the sender
of this message. (email@hidden)
This email message has been checked for the presence of computer
viruses; however this protection does not ensure this message is
virus free.
Banque centrale du Luxembourg; Tel ++352-4774-1; http://www.bcl.lu
**********************************************************************
_______________________________________________
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