0

I'm working on an IOS project that uses NSInteger to count opening and closing. The times are read at 2200 is equivalent to 10pm. I would like to see if something is within an hour of closing, so I compare the real world time in integer (works) to the closing time - 100 but the math isn't working as expected.

Custom class:

@property (assign) NSInteger *sus;
@property (assign) NSInteger *sue;

In Controller:

custom_class *location = object
resultINT = real world time in Integer with 24 hr

NSLog(@"Result INT:   %i", resultInt);
NSLog(@"location.sue:    %i", location.sue);
NSLog(@"Location - 1hr      %i", location.sue -100);
NSInteger *yellowEnd = location.sue - 100;
NSLog(@"Yellow      %i", yellowEnd);

result:

2014-07-13 18:23:51.269 Time-Genesis-Marcus[7591:416078] Result INT:   1823
2014-07-13 18:23:51.269 Time-Genesis-Marcus[7591:416078] location.sue:    2200
2014-07-13 18:23:51.269 Time-Genesis-Marcus[7591:416078] Location - 1hr      1800
2014-07-13 18:23:51.270 Time-Genesis-Marcus[7591:416078] Yellow      1800

Why isn't the third and forth lines returned 2100? thanks

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
Marcus
  • 9,032
  • 11
  • 45
  • 84

1 Answers1

5

You are doing pointer arithmetic instead of integer arithmetic.

Change your code to this to fix the problem

@property (assign) NSInteger sus;
@property (assign) NSInteger sue;

For explanation of the result you got, read Pointer Arithmetic

Basically (int *)2200 - 100 results 2200 - 100 * sizeof(int) where sizeof(int) is 4

Community
  • 1
  • 1
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143