• 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: Simple question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Simple question


  • Subject: Re: Simple question
  • From: Graham Cox <email@hidden>
  • Date: Fri, 23 Apr 2010 01:42:11 +1000

On 23/04/2010, at 1:36 AM, Arnold Nefkens wrote:

> NSNumber *fieldOne = [managedObject valueForKey:@"key1"];
> NSNumber *fieldTwo = [managedObject valueForKey:@"key2"];
> NSNumber *fieldThree = [managedObject valueForKey:@"key3"];
> if (fieldTwo > 0) {
> 	NSNumber *answer = ((fieldTwo - fieldOne) + fieldThree);
> 	[self.managedObject setValue:[answer intValue] forKey:@"key4"];
> }
>
> If I debug the right values are being shown in the fieldOne, fieldTwo and fieldThree. But the calculation answer is all the time out of scope.....
>
> What am I missing?


NSNumbers are objects that CONTAIN the value to be added, subtracted, etc. They cannot be added or subtracted directly.

You need to extract the value in the form you want it using methods such as -floatValue, -intValue and so on before doing calculations. Unfortunately perhaps, Obj-C does not allow operator overloading to allow direct use of objects in math expressions.

--Graham


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Simple question (From: Arnold Nefkens <email@hidden>)

  • Prev by Date: Simple question
  • Next by Date: Re: Simple question
  • Previous by thread: Simple question
  • Next by thread: Re: Simple question
  • Index(es):
    • Date
    • Thread