Re: Bindings, NSNumber equal to NIL and performing mathematical operations issue
Re: Bindings, NSNumber equal to NIL and performing mathematical operations issue
- Subject: Re: Bindings, NSNumber equal to NIL and performing mathematical operations issue
- From: Eric Hochmeister <email@hidden>
- Date: Thu, 5 May 2005 19:43:54 -0400
Hi Ricky,
> Note that you should be careful when sending messages to null such
> that the return type isn't an object, but some primitive type.
>
> Thus, you should rewrite result to do the guard against num1 and/or
> num2 being nil.
Thanks for the reply, I thought of doing the guard against num1 and/or
num2 being nil, but I'm finding it hard to believe that this issue has
not already been addressed somehow differently. It just seems like
alot of work to have to check if an NSNumber is nil *every* time I
want to do a calculation with it.
Is there really no other way?
Eric
On 5/5/05, Ricky Sharp <email@hidden> wrote:
>
> On May 5, 2005, at 4:52 PM, Eric Hochmeister wrote:
>
> > I'm using bindings and I'm having an issue with binding a NSNumber up
> > to an NSTextField and using this NSNumber to perform mathematical
> > operations.
> >
> > ie. I have a simple example, two textfields which are bound to two
> > NSNumber attributes and another textfield which displays the result of
> > these two NSNumbers multiplied together.
> >
> > My issue arises, when I enter valid inputs into both textfields, ie.
> > 10 and 10.. the result is 100... but if I delete 10 from one of the
> > textfields, the result still stays 100. But if I change the textfield
> > back to another number it is updated correctly. The issue I'm seeing
> > is that when my NSNumber is set to nil, and I ask [NSNumber
> > doubleValue] it's giving me not 0... causing my calculation to be
> > wrong.
> >
> > I've pasted my result method below. num1 and num2 are both bound to
> > the textfield, and result is setup to be updated when num1 or num2
> > change...
> >
> > ie. [self setKeys:[NSArray arrayWithObjects:@"num1",@"num2",nil]
> > triggerChangeNotificationsForDependentKey:@"result"];
> >
> > Any ideas? What I want is that if nothing is entered in the textfield
> > instead of saving nil, 0 is saved and the textfield is updated to
> > display 0 as well.
> >
> > - (NSNumber*)result;
> > {
> > double n1 = [[self num1] doubleValue];
> > double n2 = [[self num2] doubleValue];
> > double aTotal = n1 * n2;
> >
> > return [NSNumber numberWithDouble:aTotal];
> > }
>
> Note that you should be careful when sending messages to null such
> that the return type isn't an object, but some primitive type.
>
> Thus, you should rewrite result to do the guard against num1 and/or
> num2 being nil.
>
> Additionally, you should set up the appropriate placeholder values in
> your nib file for the two operand text fields.
>
> --
> Rick Sharp
> Instant Interactive(tm)
>
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden