-2

I have a NSNumber and when I try to do basic arithmetic it fails.

NSNumber *contentExpirationInDays = [NSNumber numberWithInt: 1];

When I try to do a basic multiplication to convert to seconds it gives me crazy numbers:

NSTimeInterval contentExpirationInSecs =  [contentExpirationInDays intValue] * 24 * 60 * 60;

Note: The next bit is now irrelevant as explained in some answers:

When I run the following commands in the console I get the following results:

(lldb) po [contentExpirationInDays intValue]
1

(lldb) po [contentExpirationInDays intValue] * 2
-148319360

UPDATE

Thanks for the information regarding po vs p. I didn't know that detail...

So here is the real problem that I am experiencing:

NSNumber *contentExpirationInDays = [[NSNumber numberWithInt:1];
NSTimeInterval contentExpiration = ([contentExpirationInDays intValue] * 24 * 60 * 60);
myObj.expirationDate = [[NSDate date] dateByAddingTimeInterval:contentExpiration];

obj.expirationDate usually ends up with many years in the future or many years in the past instead of one day ahead of today.

agarcian
  • 3,909
  • 3
  • 33
  • 55

2 Answers2

1

po is asking the debugger to print the description of the object at the address specified by the given pointer. As Martin R said, use p instead for just printing out scalar values:

p [contentExpirationInDays intValue] * 2
norders
  • 1,160
  • 9
  • 13
-1

NSNumber is a base class for objects which represent numbers, not a primitive scalar value such as a int or float.

If you need your number as an object, use NSDecimalNumber instead. Otherwise just leave it as a scalar type.

drekka
  • 20,957
  • 14
  • 79
  • 135