• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Bindings, NSNumber equal to NIL and performing mathematical operations issue
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bindings, NSNumber equal to NIL and performing mathematical operations issue


  • Subject: Re: Bindings, NSNumber equal to NIL and performing mathematical operations issue
  • From: Bob Ippolito <email@hidden>
  • Date: Thu, 5 May 2005 20:48:42 -0400

On May 5, 2005, at 5: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];
}


Why don't you make these ivars and the signature of result all doubles? That way, you don't have to screw with NSNumber at all. KVC should do the right thing.


-bob

_______________________________________________
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


References: 
 >Bindings, NSNumber equal to NIL and performing mathematical operations issue (From: Eric Hochmeister <email@hidden>)

  • Prev by Date: Re: Handling open as a result of spotlight search
  • Next by Date: Re: Handling open as a result of spotlight search
  • Previous by thread: Re: Bindings, NSNumber equal to NIL and performing mathematical operations issue
  • Next by thread: Handling open as a result of spotlight search
  • Index(es):
    • Date
    • Thread