我试图做一件非常简单的事情,但我无法弄清楚如何实现;
NSInteger * a=10;
a=a-1;
NSlog(@"a=%d",a);
由于某些原因,它显示为 a=6
。
为什么会这样呢?
我试图做一件非常简单的事情,但我无法弄清楚如何实现;
NSInteger * a=10;
a=a-1;
NSlog(@"a=%d",a);
由于某些原因,它显示为 a=6
。
为什么会这样呢?
a
声明为指针。
大多数 Objective-C 变量都是指针,但 NSInteger
是一个例外,因为它只是一个 typedef
,代表 int
或 long
。NSInteger a=10;
a=a-1;
NSlog(@"a=%d",a);
sizeof(NSInteger) == 4
),将其向后移动-1或者说向后移动一个结构体大小,则指针会减少4个字节。
这种机制在 C 中迭代结构体数组时得到了广泛应用。CGPoint myPoints[4];
CGPoint* point = myPoints; //get the first point
for (NSUInteger i = 0; i < 4; i++) {
CGPoint currentPoint = *point;
point++; //moves to the next point, adding sizeof(CGPoint)
}
pointer + 1
,还有point[1]
或1[point]
(最后两个实际上等于*(pointer + 1)
)。NSRect
、NSPoint
、CGRect
、CGPoint
、CGColor
(虽然通常用作CGColorRef
(指向结构体的指针)等)。 - David Rönnqvist
NSType t = NSFunction([NSClass methodWithConstant:NSConstant | NSConstant2])
- dreamlax