我发现一个演示代码片段,其中使用了类型转换,如下所示:(int)view。'view'是UIView对象的指针。我从来没有知道它可以用于类型转换。有人可以帮我解释一下吗?
粘贴代码在此处
- (CGPoint)accelerationForView:(UIView *)view
{
// return
CGPoint accelecration;
// get acceleration
NSValue *pointValue = [self._accelerationsOfSubViews objectForKey:
[NSNumber numberWithInteger:(int)view]];
if (pointValue == nil) {
accelecration = CGPointZero;
}
else {
[pointValue getValue:&accelecration];
}
return accelecration;
}
- (void)willRemoveSubview:(UIView *)subview
{
[self._accelerationsOfSubViews removeObjectForKey:
[NSNumber numberWithInt:(int)subview]];
}
sizeof(int)
不能保证等于sizeof(id)
,所以有这个问题。事实上,在64位的Mac上,它明确地不相等。 - Jonathan Grynspan[NSNumber numberWithUnsignedLongLong:(uintptr_t)theObject];
更好的方法是使用视图作为目标对象的objc_get/setAssociatedObject()
。 - Jonathan Grynspan