Simple question

  • Hello list,

    Im quite new, and probably missing something.

    I want to do a calculation of three fields inside of a entity in CoreData.

    Below is the code, what is wrong?

    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?

    hope someone can help...

    Arnold Nefkens
  • 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
  • Try this:

    NSNumber *fieldOne = [managedObject valueForKey:@"key1"];
    NSNumber *fieldTwo = [managedObject valueForKey:@"key2"];
    NSNumber *fieldThree = [managedObject valueForKey:@"key3"];
    NSNumber *answer;

    if (fieldTwo > 0) {
    answer = ((fieldTwo - fieldOne) + fieldThree);
    [self.managedObject setValue:[answer intValue] forKey:@"key4"];
    }

    -koko
  • On 23/04/2010, at 1:59 AM, <koko...> wrote:

    > Try this:

    Looks equally incorrect to me. The problem is that NSNumbers are not NUMBERS.

    --Graham
  • Ok.  I was just putting answer in a broader scope as the  OP indicated
    that was the problem.

    I see I must look into NSNumber ... Thx !

    -koko

    On Apr 22, 2010, at 10:11 AM, Graham Cox wrote:

    >
    > On 23/04/2010, at 1:59 AM, <koko...> wrote:
    >
    >> Try this:
    >
    >
    > Looks equally incorrect to me. The problem is that NSNumbers are not
    > NUMBERS.
    >
    > --Graham
    >
    >
    >
  • Hi,

    correct me if I'm wrong, but what you're doing is a calculation with
    the pointers ( * of NSNumber) not the values. You have to calculate it
    like this:

    NSInteger fieldOne     = [ [ managedObject valueForKey:@"key1"]
    intValue]; // if it's an integer
    NSInteger  fieldTwo     = [ [ managedObject valueForKey:@"key2"] intValue];
    NSInteger  fieldThree = [ [ managedObject valueForKey:@"key3"]
    intValue];

    if ( fieldTwo > 0 )
    {
    NSNumber *answer = [ NSNumber numberWithInt:fieldTwo - fieldOne +
    fieldThree];
    [ self.managedObject setValue:answer forKey:@"key4"];
    // maybe you need to this instead
    [ self.managedObject setValue:[ answer stringValue] forKey:@"key4"];
    }

    Don't know if intValue in this context works, if not then use this
    alternative, which must work in any way:

    Your code and change the following lines:

    NSNumber *answer = [ NSNumber numberWithInt: [fieldTwo intValue] -
    [fieldOne intValue] + [fieldThree intValue] ];

    [ self.managedObject setValue:answer forKey:@"key4"];
    //or
    [ self.managedObject setValue:[ answer stringValue] forKey:@"key4"];
    //stringValue to present it in a textfield

    Hope that helped... MacMeideln

    Am 22.04.2010 um 17:36 schrieb Arnold Nefkens:

    > Hello list,
    >
    > Im quite new, and probably missing something.
    >
    > I want to do a calculation of three fields inside of a entity in
    > CoreData.
    >
    > Below is the code, what is wrong?
    >
    > 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?
    >
    > hope someone can help...
    >
    > Arnold Nefkens
  • On 22 Apr 2010, at 17:09, Graham Cox wrote:

    >
    > On 23/04/2010, at 1:59 AM, <koko...> wrote:
    >
    >> Try this:
    >
    >
    > Looks equally incorrect to me. The problem is that NSNumbers are not NUMBERS.
    >
    If the OP has prior experience with c++ and operator overloading then their confusion might be somewhat explained.

    NSDecimalNumber includes arithmetic methods but you get nothing like c++'s operator convenience.

    Regards

    Jonathan Mitchell

    Developer
    http://www.mugginsoft.com
  • Hi Thanks

    Yeah the keys are ints and this helps a lot.

    Thanks again...

    On 22 apr 2010, at 18:31, Reinhard Segeler wrote:

    > Hi,
    >
    > correct me if I'm wrong, but what you're doing is a calculation with the pointers ( * of NSNumber) not the values. You have to calculate it like this:
    >
    > NSInteger fieldOne     = [ [ managedObject valueForKey:@"key1"] intValue]; // if it's an integer
    > NSInteger  fieldTwo     = [ [ managedObject valueForKey:@"key2"] intValue];
    > NSInteger  fieldThree = [ [ managedObject valueForKey:@"key3"] intValue];
    >
    > if ( fieldTwo > 0 )
    > {
    > NSNumber *answer = [ NSNumber numberWithInt:fieldTwo - fieldOne + fieldThree];
    > [ self.managedObject setValue:answer forKey:@"key4"];
    > // maybe you need to this instead
    > [ self.managedObject setValue:[ answer stringValue] forKey:@"key4"];
    > }
    >
    > Don't know if intValue in this context works, if not then use this alternative, which must work in any way:
    >
    > Your code and change the following lines:
    >
    > NSNumber *answer = [ NSNumber numberWithInt: [fieldTwo intValue] - [fieldOne intValue] + [fieldThree intValue] ];
    >
    > [ self.managedObject setValue:answer forKey:@"key4"];
    > //or
    > [ self.managedObject setValue:[ answer stringValue] forKey:@"key4"];
    > //stringValue to present it in a textfield
    >
    > Hope that helped... MacMeideln
    >
    > Am 22.04.2010 um 17:36 schrieb Arnold Nefkens:
    >
    >> Hello list,
    >>
    >> Im quite new, and probably missing something.
    >>
    >> I want to do a calculation of three fields inside of a entity in CoreData.
    >>
    >> Below is the code, what is wrong?
    >>
    >> 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?
    >>
    >> hope someone can help...
    >>
    >> Arnold Nefkens
    >
  • You should really use 'integerValue' rather than 'intValue' if you're otherwise using NSIntegers.  'integerValue' will return an NSInteger (which may be 32 or 64 bits wide depending on your platform), but 'intValue' will return a C 'int' (which is always 32 bit on the Mac and iPhone, regardless of the 'natural' bit width of the platform).

    Jamie.

    On 22 Apr 2010, at 18:21, Arnold Nefkens wrote:

    > Hi Thanks
    >
    > Yeah the keys are ints and this helps a lot.
    >
    > Thanks again...
    >
    >
    > On 22 apr 2010, at 18:31, Reinhard Segeler wrote:
    >
    >> Hi,
    >>
    >> correct me if I'm wrong, but what you're doing is a calculation with the pointers ( * of NSNumber) not the values. You have to calculate it like this:
    >>
    >> NSInteger fieldOne    = [ [ managedObject valueForKey:@"key1"] intValue]; // if it's an integer
    >> NSInteger  fieldTwo  = [ [ managedObject valueForKey:@"key2"] intValue];
    >> NSInteger  fieldThree = [ [ managedObject valueForKey:@"key3"] intValue];
    >>
    >> if ( fieldTwo > 0 )
    >> {
    >> NSNumber *answer = [ NSNumber numberWithInt:fieldTwo - fieldOne + fieldThree];
    >> [ self.managedObject setValue:answer forKey:@"key4"];
    >> // maybe you need to this instead
    >> [ self.managedObject setValue:[ answer stringValue] forKey:@"key4"];
    >> }
    >>
    >> Don't know if intValue in this context works, if not then use this alternative, which must work in any way:
    >>
    >> Your code and change the following lines:
    >>
    >> NSNumber *answer = [ NSNumber numberWithInt: [fieldTwo intValue] - [fieldOne intValue] + [fieldThree intValue] ];
    >>
    >> [ self.managedObject setValue:answer forKey:@"key4"];
    >> //or
    >> [ self.managedObject setValue:[ answer stringValue] forKey:@"key4"];
    >> //stringValue to present it in a textfield
    >>
    >> Hope that helped... MacMeideln
    >>
    >> Am 22.04.2010 um 17:36 schrieb Arnold Nefkens:
    >>
    >>> Hello list,
    >>>
    >>> Im quite new, and probably missing something.
    >>>
    >>> I want to do a calculation of three fields inside of a entity in CoreData.
    >>>
    >>> Below is the code, what is wrong?
    >>>
    >>> 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?
    >>>
    >>> hope someone can help...
    >>>
    >>> Arnold Nefkens
    >>

  • This is an interesting exercise! I would extract the values of the
    fields, manipulate them, then assign the contents of Field4 with the
    answer:
    int val1 = [managedObject valueForKey:@"key1"] // etc. for each user
    entry field1-3

    int val4 = val1 + etc.  // equation with final assignment to value4
    (not actual managed object)

    [managedObject valueForKey:@"key4" setValue:[NSNumber
    numberWithInt:val4]  // final assigning of val4 to managed Object
    field4
  • On Apr 22, 2010, at 10:21, Arnold Nefkens wrote:

    > Yeah the keys are ints and this helps a lot.
    >
    > Thanks again...
    >
    > On 22 apr 2010, at 18:31, Reinhard Segeler wrote:
    >
    >> Hi,
    >>
    >> correct me if I'm wrong, but what you're doing is a calculation with the pointers ( * of NSNumber) not the values. You have to calculate it like this:
    >>
    >> NSInteger fieldOne     = [ [ managedObject valueForKey:@"key1"] intValue]; // if it's an integer

    A minor correction. This should either be:

    int fieldOne = [ [ managedObject valueForKey:@"key1"] intValue];

    or:

    NSInteger fieldOne = [ [ managedObject valueForKey:@"key1"] integerValue];

    Otherwise the code is not quite portable between 32- and 64-bit architectures.
previous month april 2010 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today