Objective C整数数学错误(2200 - 100 = 1800) - 不确定原因

Mar*_*cus 0 objective-c ios

我正在开发一个使用NSInteger计算开启和关闭的IOS项目.在2200读取的时间相当于晚上10点.我想知道是否有一些东西在关闭后的一个小时内,所以我将整数(工作)中的实际世界时间与结束时间进行比较 - 100但数学运算没有按预期工作.

自定义类:

@property (assign) NSInteger *sus;
@property (assign) NSInteger *sue;
Run Code Online (Sandbox Code Playgroud)

在控制器中:

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);
Run Code Online (Sandbox Code Playgroud)

结果:

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
Run Code Online (Sandbox Code Playgroud)

为什么第三和第四行没有返回2100?谢谢

Bry*_*hen 5

你正在做指针运算而不是整数运算.

将代码更改为此可以解决问题

@property (assign) NSInteger sus;
@property (assign) NSInteger sue;
Run Code Online (Sandbox Code Playgroud)

为了解释你得到的结果,请阅读Pointer Arithmetic

基本上(int *)2200 - 100导致2200 - 100 * sizeof(int)其中sizeof(int)是4